Sort column vectors of a matrix and extract a new vector from a maximal criterion
1 view (last 30 days)
Show older comments
I do have a huge nx4 matrix M in which its column vectors are listed according to given categories. All column vectors from the same category have their first 2 row elements the same(below Example shows 5 I-to V distinct categories) . I need to extract another line vector V built only from the maximal values on the fourth row elements of each vector category(shown with asterisc below them *).V has same elements as nr. of categories. I need to extract V vector
M
<----cat I--------> <----cat II--------> <---cat III----> <----cat IV-----> <---cat III---->
1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 _
1 1 1 1 2 2 2 2 4 4 4 1 1 1 2 2 2 _ 2Rows define category vector
1 2 3 4 1 2 3 4 1 2 3 2 3 4 1 2 3
---------------------------------------------------------------------------------------------------------
31 16 15 9 10 8 13 6 16 11 4 9 4 10 6 17 15 Row with maximal criterion to sort
* * * * * Max value of fourth row of each category
M=[1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2;1,1,1,1,2,2,2,2,4,4,4,1,1,1,2,2,2;1,2,3,4,1,2,3,4,1,2,3,2,3,4,1,2,3;31,16,15,9,10,8,13,6,16,11,4,9,4,10,6,17,15];
V=[31,13,16, 10, 17];
3 Comments
Accepted Answer
Stephen23
on 20 Aug 2021
Edited: Stephen23
on 20 Aug 2021
M = [1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2; 1,1,1,1,2,2,2,2,4,4,4,1,1,1,2,2,2; 1,2,3,4,1,2,3,4,1,2,3,2,3,4,1,2,3; 31,16,15,9,10,8,13,6,16,11,4,9,4,10,6,17,15];
[~,~,X] = unique(M(1:2,:).','rows','stable');
V = accumarray(X(:),M(4,:),[],@max)
or
Y = splitapply(@max,M(4,:).',X(:))
Your data would be easier to work with if it were oriented as an Nx4 matrix.
0 Comments
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!