Azzera filtri
Azzera filtri

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

1 visualizzazione (ultimi 30 giorni)
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

Risposta accettata

Andrei Bobrov
Andrei Bobrov il 22 Ago 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 Commenti
abdul wahab  aziz
abdul wahab aziz il 23 Ago 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 il 23 Ago 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{:}),:);

Accedi per commentare.

Più risposte (1)

Azzi Abdelmalek
Azzi Abdelmalek il 22 Ago 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

Categorie

Scopri di più su Structures in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by