How can I sort a matrix elementwise ?

2 次查看(过去 30 天)
Hi, I need to sort a matrix elementwise and get the results to a single vector without losing the index of each element.
For example,
A = [3, 4 ; 6, 2]
The sorted vector should be ,
B = [2;3;4;6]
without losing the information of each and every element in the original matrix.
Thank you.

采纳的回答

Andrei Bobrov
Andrei Bobrov 2019-10-21
A = [3, 4 ; 6, 2];
[m,~] = size(A);
[B,i] = sort(A(:));
index = [mod(i-1,m) + 1, ceil(i/m)];

更多回答(1 个)

Stephan
Stephan 2019-10-21
B = sort(reshape(A,[],1))
  3 个评论
Stephan
Stephan 2019-10-21
[B, idx] = sort(reshape(A,[],1))
[row,col] = ind2sub([size(A,1), size(A,2)],idx)
Gayan Lankeshwara
Gayan Lankeshwara 2019-10-21
Hi Stephan,
This is really what I needed and the inbuilt in2sub function is more powerful I guess.
Thanks.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

产品


版本

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by