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
source
share