Sort a matrix with another matrix

조회 수: 26 (최근 30일)
Bi
Bi 2017년 10월 16일
댓글: Andrei Bobrov 2019년 4월 29일
How can I sort a matrix A with another matrix B, where the elements of B may be replicated (not in a complete sequence of 1:n with all numbers of 1,2,3,...,n-1,n)? For example,
If I have a matrix A=[5 1; 8 2; 7 3; 3 4; 2 5];
and a matrix of B=[0; 0; 2; 1; 1];
(Or a matrix of B1 where B1=[0 0; 0 0; 2 2; 1 1; 1 1];)
How can I sort A with B with answer of resultant matrix C of C=[5 1; 8 2; 2 5; 3 4; 7 3];?

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2017년 10월 16일
[~,id]=sort(B)
C=A(id,:)
  댓글 수: 2
Paul Strickland
Paul Strickland 2018년 7월 27일
What do the ~ and the id represent in this command?

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

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2017년 10월 16일
[~,ii] = sortrows([B,A(:,1)])
C = A(ii,:)
  댓글 수: 2
Evans Gyan
Evans Gyan 2019년 4월 29일
Under the same problem, I would want to sort or match the vectors in A with that of B.
Thank you in advance !
A = 64.3169
49.8369
54.1163
B = 54.5211
64.1124
50.5202
so that the output will be
C = 64.1124
50.1163
54.5211
Andrei Bobrov
Andrei Bobrov 2019년 4월 29일
[~,i1] = sort(A);
[~,i2] = sort(i1);
Bst = sort(B);
C = Bst(i2);

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

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by