flip certain columns in matrix

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

raja bala
raja bala 2017-12-10
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by