How does max choose the answers when there are two identical values in a vector

Dear All,
Could you help me with clarify this mystery please? I am using the following code to find the maximum value and its index in a vector
[rdeubest,bestcol]=max(rdeus);
In matlab documentation, it says max return the first value when there are identical values in the the vector. As the values are identical, I suppose the statement means max return the index of the first maximum value. But I find sometimes this does not seem to be true. For example, I have
rdeus=[-100000 -100000 -100000 -100000 -100000 -100000 0.848202365959724 0.849479878997915 0.849479878997915 -100000 -100000 -100000 0.848158759181734]
Notice we have rdeus(8)=rdeus(9). But when I run the max code, I get the bestcol=9. Could you offer me some advice about why this happens?
Cheers, Xueq

 Accepted Answer

It works fine for me:
>> rdeus = [-100000,-100000,-100000,-100000,-100000,-100000,0.848202365959724,0.849479878997915,0.849479878997915,-100000,-100000,-100000,0.848158759181734];
>> [C,I] = max(rdeus)
C =
0.84948
I =
8
I suspect that your rdeus(8) and rdeus(9) are not really as similar as you think they are. Are the values generated somewhere else, or defined only at the creation of the matrix rdeus ?
Try this:
fprintf('%.20f\n',rdeus(8:9))
Are they identical? (they are for me)

3 Comments

Hi thanks. I tried fprintf and it turns out that they are not identical at the 20th decimal. Thanks for clarify this.
I then need another help. The value of rdeus are generated somewhere else. And I know that rdeus(8) should be identical to rdeus(9) by analytic analysis. Could I ask that the fact the results calculated by matlab are different is possible because of the calculation precision. If not, I then need to re-think my analytic analysis.
If it is possible, then how could I let max to return the first index (in this case is 8) to me by assuming redeus(8)=rdeus(9). I would like the result of index to be returned in consistency. I find that sometimes I get the index of the first one and sometimes I get the index of the second one. Maybe it is because of the uncertainty in calculation precision of rdeus(8) and rdeus(9) ?
The most important thing to understand about numeric calculations (such as with MATLAB) is that they are NOT analytic calculations. The propagation of small differences between the values can become quite significant, like this situation when a user expects analytic-like equality. Some fun things to read about floating point calculations:
You could try defining some kind of max with a tolerance... however this leads to some grouping issues, as is explained here:
The simplest solution may be to simply multiply the values by 1e15 (picking a suitable order), round to the nearest integer and then use max:
>> rdeus = [-100000,-100000,-100000,-100000,-100000,-100000,0.848202365959724,0.849479878997915,0.8494798789979153,-100000,-100000,-100000,0.848158759181734];
>> [~,I] = max(rdeus)
I =
9
>> [~,I] = max(round(rdeus*1e15))
I =
8
>> C = rdeus(I);
But the answer really depends on you: only you know how similar these values must be to count as being "the same".
Does that resolve your situation? If it does, clicking "Accept" is always appreciated. If it doesn't, please explain what remains unclear.

Sign in to comment.

More Answers (0)

Tags

Asked:

on 4 Jan 2015

Commented:

on 4 Jan 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!