Extract the Maximum Column elements of every Row
1 次查看(过去 30 天)
显示 更早的评论
Hello , I have an array
A = [8 4; 3 6; 2 7; 1 4; 2 3;2 1;3 1; 3 5; 8 6; 8 1];
I want to extract maximum column elements for every Row.
In the above array A i have
3 1 ; 3 5 ; 3 6
I want only 3 6 to be retained as that is the maximum column for that row.
Similarly i have 8 1 ;8 4; 8 6 I want only 8 6 as that has the maximum column value.
Final output I want for the array A is as follows
ans =
1 4;
2 7;
3 6;
8 6;
Looking forward to hear from you at the earliest
Thanks
Pankaja
0 个评论
采纳的回答
Guillaume
2015-12-19
Your wording is rather poor. It looks like what you want to do is for each unique value in the first column find the corresponding maximum in the second column. This is trivial to obtain with unique and accumarray:
A = [8 4; 3 6; 2 7; 1 4; 2 3;2 1;3 1; 3 5; 8 6; 8 1];
[val, ~, subs] = unique(A(:, 1));
B = [val, accumarray(subs, A(:, 2), [], @max)]
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!