Reputation: 23
I have the following matrix:
[ 2 5 7 8 1 3 4 6 5 7 3 1;
1 1 1 1 2 2 2 2 3 3 3 3;]
The first row represents values and the second characteristic
I want to get the max value if the value in the second row is the same, i.e. their characteristic is the same. So, what I would like to have is:
[ 8 6 7]
, since 8
is the highest value when the second row is 1
, 6
when the second row is is 2
, and 7
when the second row is 3
. I can do it with a loop, but I would like vectorized solution, and if possible of course, in one line.
Upvotes: 1
Views: 49
Reputation: 36710
accumarray
does exactly what you want
x=[ 2 5 7 8 1 3 4 6 5 7 3 1; 1 1 1 1 2 2 2 2 3 3 3 3;]
accumarray(x(2,:)',x(1,:)',[],@max)
Upvotes: 3