Matrix row swapping with respect to column
Show older comments
How to swap between two rows of matrix with respect to column? For example: matrix A is: 1 3 4; 2 4 5 and Y want to be 1 4 5;2 3 4 , only 2nd and 3rd coulm row values have to be interchanged.? I want a generalised equation so that when I input the column no,numbers between the said column to end in a particular row has to be swapped with the next row.
Answers (2)
KALYAN ACHARJYA
on 27 Jul 2019
Edited: KALYAN ACHARJYA
on 27 Jul 2019
"only 2nd and 3rd coulm row valuese have to be interchanged.?"
Y=[1 3 4; 2 4 5]
Z=Y;
Y(:,2)=Y(:,3);
Y(:,3)=Z(:,2);
Y
Result:
Y =
1 3 4
2 4 5
Y =
1 4 3
2 5 4
madhan ravi
on 27 Jul 2019
I have no idea why Kalyan is making an easy task really harder but here is what you want:
Y = A;
Y(:,[2,3]) = flip(Y(:,[2,3]))
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!