# How to do column or row swapping or interchange in a matrix?

5 views (last 30 days)
SANDEEP SINGH RANA on 26 Dec 2021
Commented: Stephen23 on 27 Dec 2021
Hi,
IFor example, In a matrix A = 3x5 , I want to swap complete column 2 with column 5.
How can i do that. Is there any in-built function or code?
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Yusuf Suer Erdem on 26 Dec 2021
Try these codes below please;
A = [1, 2, 3, 4, 5; 4, 5, 6, 7, 8; 9, 10, 11, 12, 13]
v = A(:, 2);
A(:, 2) = A(:, 5);
A(:, 5) = v;
A
##### 2 CommentsShowHide 1 older comment
Yusuf Suer Erdem on 27 Dec 2021
Hello use my codes below please. If this answer does your job, accept my answer please. After my research, i could not find a specific function just for this purpose.
A=[1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15];
A
A = A(:,[1 5 3 4 2])

Sign in to comment.

### More Answers (1)

Voss on 27 Dec 2021
A(:,[5 2]) = A(:,[2 5]);
##### 1 CommentShowHide None
Stephen23 on 27 Dec 2021
+1 neat and simple

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!