how to find the indices after sorting columns of a matrix

2 次查看(过去 30 天)
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