# Change column and row

201 views (last 30 days)
nur yusof on 22 Feb 2016
Commented: nur yusof on 22 Feb 2016
Hi, how can I change the positions of entire row and columns in a matrix [nxm]? For example, I want to change my entire 10th row and 10th column into entire 1st column and 1st row.
Thank you.

Dennie on 22 Feb 2016
Hello, You can swap columns and rows quite easily in matlab. For example:
given matrix :
matrix=[1 2 3 4 5;6 7 8 9 10; 11 12 13 14 15; 16 17 18 19 20];
i can swap column 1 and 2 by performing this operation:
matrix(:,[1,2])=matrix(:,[2,1]);
for row 1 and 2, a similar operation is done:
matrix([1,2],:)=matrix([2,1],:);
Please be aware of the sequence in which you perform this operation, since column 10 and row 10 have 1 variable in common, if you swap columns and rows sequentialy, you will mix up that one variable.
Another (manual) approach to retain information is to make a copy of the row and column you want to swap in another variable such as:
copy_matrix=matrix(:,:);
matrix(2,:)=copy_matrix(1,:);
matrix(1,:)=copy_matrix(2,:);
Hope this helps,
Dennie
nur yusof on 22 Feb 2016
Excellent.Thank you so much.

Ilham Hardy on 22 Feb 2016
Edited: Ilham Hardy on 22 Feb 2016
There are built-in matlab functions for this..
##### 2 CommentsShowHide 1 older comment
nur yusof on 22 Feb 2016
Awesome. Thank you so much.