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

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by