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 日

0 投票

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 件)

カテゴリ

ヘルプ センター および File ExchangeShifting 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