Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
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

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 22 Aug. 2016
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 Kommentare
abdul wahab  aziz
abdul wahab aziz am 23 Aug. 2016
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 am 23 Aug. 2016
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{:}),:);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Azzi Abdelmalek
Azzi Abdelmalek am 22 Aug. 2016
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

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by