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

4 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 22 de Ag. de 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 comentarios
abdul wahab  aziz
abdul wahab aziz el 23 de Ag. de 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 el 23 de Ag. de 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{:}),:);

Iniciar sesión para comentar.

Más respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 22 de Ag. de 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

Categorías

Más información sobre Structures en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by