flip certain columns in matrix

9 Ansichten (letzte 30 Tage)
john creighton
john creighton am 20 Okt. 2014
Kommentiert: Adam am 11 Dez. 2017
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?

Akzeptierte Antwort

Adam
Adam am 20 Okt. 2014
Bearbeitet: Adam am 20 Okt. 2014
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);

Weitere Antworten (1)

raja bala
raja bala am 10 Dez. 2017
A(:,[2]) = flip (A(:,[2]))
  1 Kommentar
Adam
Adam am 11 Dez. 2017
This flips the 2nd column only, as does
A(:,2) = flip (A(:,2))
without the square brackets.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Argument Definitions finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by