In general, the == operator is not suited to test for “numeric” equality, but one should rather do something like abs(a - b) < eps. However, when I want to find the location of the largest element in an array, is it save to assume that max will return the element unchanged? Is it ok to do
[row, col] = find(a == max(a(:));
Yes.
maxonly compares two values, and does not do any operations on them that might change their values.Here’s a typical C++ implementation of a
max:As you see, this function will return the exact same value as either
aorb.Matlab just adds matrix formalism, fancy formatting wrappers etc. to it, but its kernel will follow the same principles as the example above.
So yes, it is OK to use equality here.