How to convert row-major linear indices to column-major indices?

22 ビュー (過去 30 日間)
Preetham Manjunatha
Preetham Manjunatha 2022 年 3 月 23 日
I have a row-major linear indices [1,8, 14, 9, 4, 11, 18] from a matrix 3 x 6 (row x column). How to convert this to column-major linear indices [1, 5, 6, 8, 10, 14, 18] without for-loops? I want to generalize for any rectangular or square matrices.

採用された回答

Stephen23
Stephen23 2022 年 3 月 23 日
編集済み: Stephen23 2022 年 3 月 23 日
S = [3,6]; % matrix size
X = [1,8,14,9,4,11,18]; % row-major linear indices
[Y,Z] = ind2sub(flip(S),X);
V = sub2ind(S,Z,Y) % column-major linear indices
V = 1×7
1 5 6 8 10 14 18
  4 件のコメント
Stephen23
Stephen23 2022 年 3 月 24 日
S = [3,6]; % matrix size
X = [1,5,6,8,10,14,18]; % column-major linear indices
[Y,Z] = ind2sub(S,X);
V = sub2ind(flip(S),Z,Y) % row-major linear indices
V = 1×7
1 8 14 9 4 11 18
Preetham Manjunatha
Preetham Manjunatha 2022 年 3 月 24 日
Thanks!

サインインしてコメントする。

その他の回答 (1 件)

Arif Hoq
Arif Hoq 2022 年 3 月 23 日
編集済み: Arif Hoq 2022 年 3 月 23 日
try reshape function:
A=[1 2 3 4 5 6; 7 8 9 10 11 12;13 14 15 16 17 18] % any matrix
A = 3×6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
D=reshape(A,[],3)
D = 6×3
1 3 5 7 9 11 13 15 17 2 4 6 8 10 12 14 16 18

カテゴリ

Help Center および File ExchangeTroubleshooting Linearization Results についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by