How can I sort a matrix elementwise ?

조회 수: 7(최근 30일)
Gayan Lankeshwara
Gayan Lankeshwara 2019년 10월 21일
댓글: Gayan Lankeshwara 2019년 10월 21일
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
Gayan Lankeshwara
Gayan Lankeshwara 2019년 10월 21일
Hi Andrei,
I tried the code and this is what I wanted.
Thank you.

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Stephan
Stephan 2019년 10월 21일
B = sort(reshape(A,[],1))
  댓글 수: 3
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.

댓글을 달려면 로그인하십시오.

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by