Check if arrays are proportional

Is there a good way to check if two arrays are proportional in MATLAB? Something like a function isequal, but for proportionality testing.

+4
source share
3 answers

One heuristic way to do this is to simply split one array into another element and make sure that the largest and smallest values ​​in this result are within a certain tolerance. A degenerate case is when you have zeros in arrays. In this case, the use maxand mindoes not affect the operation of the algorithm, since these functions ignore values nan. However, if the two A and Bare null arrays, there is an infinite number of scalar multiples that are possible, and therefore there is no answer. We will set it on nanif we meet it.

Given Aand B, something like this might work:

C = A./B; % Divide element-wise
tol = 1e-10; % Divide tolerance

% Check if the difference between largest and smallest values are within the
% tolerance
check = abs(max(C) - min(C)) < tol;

% If yes, get the scalar multiple
if check
    scalar = C(1);
else % If not, set to NaN
    scalar = NaN;
end
+5
source

, pdist2 'cosine' . 0, :

>> pdist2([1 3 5], [10 30 50], 'cosine')
ans =
     0

>> pdist2([1 3 5], [10 30 51], 'cosine')
ans =
     3.967230676171774e-05

@rayryeng, , .

+3
A = rand(1,5);
B = pi*A;
C = A./B; %Divide the two
PropArray = all(abs(diff(C))<(3*eps)); % check for equality within tolerance
if PropArray
    PropConst = C(1); % they're all equal, get the constant
else
    PropConst = nan; % They're not equal, set nan
end
+2
source

All Articles