Find Max value with index

5 ビュー (過去 30 日間)
iam
iam 2018 年 3 月 8 日
編集済み: Stephen23 2018 年 3 月 8 日
Hello All, I have two vectors of size (100x1) named "groups" and "norms", groups vector contains labels such as
groups=[1 2 2 1 3 2 3]
and norms vector contains length of vectors that belongs to one of the groups.
norms=[0.4923 0.6023 0.5717 0.4296 0.5425 0.5101 0.5185]
Now I want to find the max value of norms in each group with their corresponding index position in norms vector. I have the following code which gets the max norm in each group but it does not return index.
maxs = accumarray(groups(:,1), norms(:,1), [], @max);
Any help will be appreciated. Thanks.

回答 (1 件)

Rik
Rik 2018 年 3 月 8 日
Your current code doesn't return a maximum value for each group. The code below should do what you need.
groups=[1 2 2 1 3 2 3];
norms=[0.4923 0.6023 0.5717 0.4296 0.5425 0.5101 0.5185];
grouplist=unique(groups);
num=numel(grouplist);
maxs=zeros(num,1);
maxs_idx=zeros(num,1);
for idx=1:num
[maxs(idx),maxs_idx(idx)]=max(norms(groups==grouplist(idx)));
end
  2 件のコメント
iam
iam 2018 年 3 月 8 日
Thanks for your response, but as I checked its not giving me the correct maximum values of each group.
Stephen23
Stephen23 2018 年 3 月 8 日
編集済み: Stephen23 2018 年 3 月 8 日
@iam: please show us what the required output is. Testing code is very difficult if the required output is not known to us.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by