Sort column vectors of a matrix and extract a new vector from a maximal criterion
1 次查看(过去 30 天)
显示 更早的评论
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];
采纳的回答
Stephen23
2021-8-20
编辑:Stephen23
2021-8-20
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!