フィルターのクリア

how to find maximum value in some specific group range of matrix

1 回表示 (過去 30 日間)
abdul wahab  aziz
abdul wahab aziz 2016 年 8 月 22 日
コメント済み: Andrei Bobrov 2016 年 8 月 23 日
i have a matrix
3 2 5 6,
4 2 5 5,
5 2 5 3,
6 2 5 4,
7 2 5 1,
8 2 5 1,
9 2 5 3,
11 2 5 1,
2 3 8 6,
4 3 8 3,
5 3 8 3,
in which last column represents its count so i want a complete row which has max count answer for its 2nd and 3rd column grouping e.g 2 5 is a group here and max count is 6 so i must get 3 2 5 6., second group is 3 8 so for this i must get ans 2 3 8 6

採用された回答

Andrei Bobrov
Andrei Bobrov 2016 年 8 月 22 日
a = [3 2 5 6,
4 2 5 5,
5 2 5 3,
6 2 5 4,
7 2 5 1,
8 2 5 1,
9 2 5 3,
11 2 5 1,
2 3 8 6,
4 3 8 3,
5 3 8 3];
[~,~,c] = unique(a(:,2:3),'rows');
[ii] = accumarray(c,(1:size(a,1))',[],@(x)x(a(x,4)==max(a(x,4))));
out = a(ii,:);
  2 件のコメント
abdul wahab  aziz
abdul wahab aziz 2016 年 8 月 23 日
THIS WORKS FINE BUT THE PROBLEM IS IF COUNT IS SAME IT CALLS FIRST MAX COUNTED ROW WHERE AS IT MUST REPEAT IF THERE IS SAME COUNT
Andrei Bobrov
Andrei Bobrov 2016 年 8 月 23 日
a = [3 2 5 6,
4 2 5 5,
5 2 5 3,
6 2 5 4,
7 2 5 1,
8 2 5 1,
9 2 5 3,
11 2 5 1,
2 3 8 6,
4 3 8 3,
12 3 8 6 % repeated max volue
5 3 8 3];
[~,~,c] = unique(a(:,2:3),'rows');
f = @(x){x(a(x,4)==max(a(x,4)))};
ii = accumarray(c,(1:size(a,1))',[],f);
out = a(cat(1,ii{:}),:);

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 22 日
A=[3 2 5 6; 4 2 5 5; 5 2 5 3; 6 2 5 4; 7 2 5 1; 8 2 5 1; 9 2 5 3; 11 2 5 1; 2 3 8 6; 4 3 8 3; 5 3 8 3]
[ii,jj,kk]=unique(A(:,2:3),'rows','stable')
f=accumarray(kk,(1:numel(kk))',[],@(x) {A(x,:)})
for k=1:numel(f)
[~,idx]=max(f{k}(:,4))
out(k,:)=f{k}(idx,:)
end

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by