how to find the indices after sorting columns of a matrix

조회 수: 2 (최근 30일)
Danish Nasir
Danish Nasir 2022년 5월 31일
답변: SALAH ALRABEEI 2022년 5월 31일
Suppose i have 4x4 matrix A with different integers. I sort each column of the matrix i.e. B= sort (A,'ascend'). Now i want matrix C which should show changed indices of matrix A after sorting.

채택된 답변

SALAH ALRABEEI
SALAH ALRABEEI 2022년 5월 31일
If I got you correctly, I think you want you want this
clear
A = magic(4)
A = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
[n,m]=size(A)
n = 4
m = 4
[As,Cur_ind]=sort(A,'ascend')
As = 4×4
4 2 3 1 5 7 6 8 9 11 10 12 16 14 15 13
Cur_ind = 4×4
4 1 1 4 2 3 3 2 3 2 2 3 1 4 4 1
Org_ind = reshape(1:n*m,n,m)
Org_ind = 4×4
1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16
bb=[1:n].*ones(n,m)
bb = 4×4
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
C = reshape(diag(Org_ind(Cur_ind,bb)),n,m)
C = 4×4
4 5 9 16 2 7 11 14 3 6 10 15 1 8 12 13

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by