How to find subgroup min or max

1 visualizzazione (ultimi 30 giorni)
Tiina
Tiina il 8 Mar 2016
Commentato: Stephen23 il 8 Mar 2016
group_id=[1 1 1 2 2 3 3 3]';
B=[ 5 6 7 4 1 2 2 3]';
output: C=[ 5 0 0 0 1 2 2 0]';
  1 Commento
Jan
Jan il 8 Mar 2016
The question is not clear. Please explain how C is obtained.

Accedi per commentare.

Risposta accettata

Stephen23
Stephen23 il 8 Mar 2016
Modificato: Stephen23 il 8 Mar 2016
X = [1;1;1;2;2;3;3;3];
B = [5;6;7;4;1;2;2;3];
A = accumarray(X,B,[],@min);
Y = any(bsxfun(@eq,A,B.'));
C = zeros(size(B));
C(Y) = B(Y)
where the output is
C =
5
0
0
0
1
2
2
0
  2 Commenti
Tiina
Tiina il 8 Mar 2016
great thx, although i dont know wat "any" means with the bsxfun
Stephen23
Stephen23 il 8 Mar 2016
Read the any documentation.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by