简体   繁体   English

找到最大值matlab的所有索引

[英]find all indices of max values matlab

I just want to find all the indices of the maximum value in a vector in matlab. 我只是想在matlab中找到一个向量中最大值的所有索引。 the max function returns only the index of the first occurence of the maximum. max函数仅返回最大值的第一个出现的索引。 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? 我需要indicesDesMax有2和3这是我们在maxChaqueCell中的两个5的索引,我该怎么做?

Thanks. 谢谢。

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. 这就是你如何找到所有这些,而不仅仅是第一个。

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

sortedmaximums = [value,index];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM