About row and column swapping in a matrix

13 ビュー (過去 30 日間)
Mohammad
Mohammad 2015 年 7 月 22 日
コメント済み: Rakib Seemanto 2020 年 11 月 1 日
Hi Guys. I have a question which probably could be easy or might have built in function but I kinda stuck on it. Attached is a 3x3 matrix as an image as appeared below. I want to swap the rows and columns as shown here and save each sequence of swapping. I have a very big file to handle, i have just shown a small sample here. I tried with a loop but could not get it working. I would greatly appreciate any efforts.
Thanks Rafiq

採用された回答

Star Strider
Star Strider 2015 年 7 月 22 日
編集済み: Star Strider 2015 年 7 月 22 日
I would just use circshift:
A = [0 1 2; 1 1.4 2.2; 2 2.2 2.8];
B = circshift(A, [-1 -1])
B =
1.4 2.2 1
2.2 2.8 2
1 2 0
  5 件のコメント
Star Strider
Star Strider 2015 年 7 月 23 日
My pleasure.
Rakib Seemanto
Rakib Seemanto 2020 年 11 月 1 日
how can I swap 6x6 matrix middle two rows.A = [ 1 2 3;4 5 6;7 8 9;10 11 12;13 14 15;16 17 18]
The output will be:B = [ 1 2 3;4 5 6;10 11 12;7 8 9;13 14 15;16 17 18]

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 7 月 22 日
To swap column J with column K you would use
newArray = oldArray;
newArray(:,[K J]) = newArray(:,[J K]];
For swapping rows,
newArray([K J],:) = newArray([J K],:);
  1 件のコメント
Mohammad
Mohammad 2015 年 7 月 22 日
Thanks Walter. I actually used this technique and used a loop. The problem is when you swap the columns, all the columns are swapped and same with rows. If you look at my matrix, first I want to swap all the columns (top 3 matrix), then I want to swap first row with second row and swap all the columns (middle 3 matrix), and finally I want to swap row 2 with row 3 and swap all the columns again (the last three matrix). My loop is not working the way I want it. Thanks for looking into the problem. I appreciate it.

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by