how to mind the nth max or min value of a column matrix with its location
2 views (last 30 days)
Show older comments
suppose I have one a=nXm matrix and the the function values of a is saved in another matrix b. Now I want to sort matrix a on basis of their function evaluation. or how can I get the nth max or min values of matrix b with the position.
Answers (1)
Jan
on 15 Jun 2018
a = rand(3,4);
b = sin(a);
[bs, index] = sort(b, 2); % If you want to sort along the rows
s = size(a);
indexL = sub2ind(s, repmat((1:s(1)).', 1, s(2)), index);
as = a(indexL)
2 Comments
Jan
on 16 Jun 2018
What does this mean? Are you able to transfer my suggestion to operate along the columns?
a = rand(3,4);
b = sin(a);
[bs, index] = sort(b, 1); % If you want to sort along the columns
s = size(a);
indexL = sub2ind(s, index, repmat(1:s(2), s(1), 1));
as = a(indexL)
What is "the previous location"? The less the readers have to guess, the easier and more likely matching is the answer.
See Also
Categories
Find more on Shifting and Sorting Matrices 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!