How to find subgroup min or max

1 次查看(过去 30 天)
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 个评论
Jan
Jan 2016-3-8
The question is not clear. Please explain how C is obtained.

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2016-3-8
编辑:Stephen23 2016-3-8
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 个评论
Tiina
Tiina 2016-3-8
great thx, although i dont know wat "any" means with the bsxfun

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by