find all indices of max values matlab

只谈情不闲聊 提交于 2019-12-30 08:34:23

问题


I just want to find all the indices of the maximum value in a vector in matlab. the max function returns only the index of the first occurence of the maximum. For example:

maxChaqueCell = [4     5     5     4]
[maximum, indicesDesMax] = max(maxChaqueCell)
 maximum =

         5
 indicesDesMax =

         2

I need the indicesDesMax to have 2 and 3 which are the indices of the two 5 we have in maxChaqueCell , how can I do that?

Thanks.


回答1:


First you find the max value, then you find all the elements equal to that:

m = max(myArray);
maxIndex = find(myArray == m);

Or using your variable names:

maxChaqueCell = [4 5 5 4];
maximum = max(maxChaqueCell)
indicesDesMax = find( maxChaqueCell == maximum );

This is how you find all of them, not just the first one.




回答2:


[value,index] = sort(maxChaqueCell,'descend');

sortedmaximums = [value,index];


来源:https://stackoverflow.com/questions/17126948/find-all-indices-of-max-values-matlab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!