flip certain columns in matrix

조회 수: 11 (최근 30일)
john creighton
john creighton 2014년 10월 20일
댓글: Adam 2017년 12월 11일
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
john creighton
john creighton 2014년 10월 20일
Thanks Adam. works perfectly :)

댓글을 달려면 로그인하십시오.

추가 답변 (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