一种启发式方法是简单地将一个数组除以另一个元素,并确保此结果中的最大值和最小值在一定容差范围内.退化的情况是你在数组中有零.在这种情况下,使用max
并且min
不会影响此算法的工作方式,因为这些函数会忽略nan
值.然而,如果这两个 A
和B
是零个数组,然后有标量的倍数是可能的,所以没有一个答案无限多的.nan
如果我们遇到这个问题,我们会将其设置为.
鉴于A
和B
,这样的东西可以工作:
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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
93 次 |
最近记录: |