フィルターのクリア

Please help with using accumarray to choose the max value of the array

1 回表示 (過去 30 日間)
Hashini Wanniarachchi
Hashini Wanniarachchi 2020 年 3 月 18 日
I have a matrix, for an example with 11 x 3.
CH BA OL
10.0000 10.0000 0.6000
10.0000 11.0000 0.4000
11.0000 9.0000 0.6000
11.0000 10.0000 0.2000
11.0000 11.0000 0.2000
25.0000 44.0000 0.1000
25.0000 9.0000 0.7000
25.0000 45.0000 0.2000
27.0000 44.0000 0.2000
27.0000 46.0000 0.3000
27.0000 9.0000 0.5000
I'm trying to take unique values for column CH, and each unique value in CH should sort the maximum value's BA. For example, final results should be like
CH BA OL
10 10 0.6
11 9 0.6
25 9 0.7
27 46 0.5
I was trying accumarray but I couldnt get the correct answer. Please help
  4 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 3 月 18 日
In that case, last row should be 27 9 0.5
Hashini Wanniarachchi
Hashini Wanniarachchi 2020 年 3 月 18 日
I apologize for the confusion!

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

回答 (2 件)

Mohammad Sami
Mohammad Sami 2020 年 3 月 18 日
% matrix = 11 x 3
ch = matrix(:,1);
[u,~,ic] = unique(ch);
maxba = accumarray(ic,matrix(:,2),[],@max);
out = [u maxba];
i = ismember(matrix(:,1:2),out,'rows');
out = matrix(i,:);
  1 件のコメント
Hashini Wanniarachchi
Hashini Wanniarachchi 2020 年 3 月 18 日
Thank you so much for guidance. Well I wanted to sort through col 3. I sorted it out anyhow following this. Thanks again!

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


Ameer Hamza
Ameer Hamza 2020 年 3 月 18 日
編集済み: Ameer Hamza 2020 年 3 月 18 日
Try this
M = [10.0000 10.0000 0.6000
10.0000 11.0000 0.4000
11.0000 9.0000 0.6000
11.0000 10.0000 0.2000
11.0000 11.0000 0.2000
25.0000 44.0000 0.1000
25.0000 9.0000 0.7000
25.0000 45.0000 0.2000
27.0000 44.0000 0.2000
27.0000 46.0000 0.3000
27.0000 9.0000 0.5000];
[~,idx] = sort(M(:,3));
M = M(idx,:);
[~,idx] = sort(M(:,1));
M = M(idx,:);
result = splitapply(@(x) x(end,:), M, findgroups(M(:,1)));

カテゴリ

Help Center および File ExchangeOther Formats についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by