hey all i need to flip every nth column in a matrix. ie. if i have matrix A=[1 2 3;4 5 6;7 8 9] i need it to become A=[1 8 3;4 5 6;7 2 9] any suggestions?

 採用された回答

Adam
Adam 2014 年 10 月 20 日
編集済み: Adam 2014 年 10 月 20 日

1 投票

n = 2;
colIdx = 2:n:size(A,2);
A(:,colIdx) = flip( A(:,colIdx) )
assuming you want to flip even columns (in the case of n = 2). Obviously for odd colums just change the first line to:
colIdx = 1:n:size(A,2);

1 件のコメント

john creighton
john creighton 2014 年 10 月 20 日
Thanks Adam. works perfectly :)

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

その他の回答 (1 件)

raja bala
raja bala 2017 年 12 月 10 日

0 投票

A(:,[2]) = flip (A(:,[2]))

1 件のコメント

Adam
Adam 2017 年 12 月 11 日
This flips the 2nd column only, as does
A(:,2) = flip (A(:,2))
without the square brackets.

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

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

質問済み:

2014 年 10 月 20 日

コメント済み:

2017 年 12 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by