How to find subgroup min or max

1 view (last 30 days)
Tiina
Tiina on 8 Mar 2016
Commented: Stephen23 on 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 Comment
Jan
Jan on 8 Mar 2016
The question is not clear. Please explain how C is obtained.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 8 Mar 2016
Edited: Stephen23 on 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 Comments
Tiina
Tiina on 8 Mar 2016
great thx, although i dont know wat "any" means with the bsxfun
Stephen23
Stephen23 on 8 Mar 2016
Read the any documentation.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!