how to find the indices after sorting columns of a matrix

2 views (last 30 days)
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.

Accepted Answer

SALAH ALRABEEI
SALAH ALRABEEI on 31 May 2022
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

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!