Swapping Rows & Columns of Matrix in MATLAB
Swapping rows & columns of Matrix is a very important work in iterative methods of Linear Algebra or Image Processing, e.g, in Jacobi iteration method if the Matrix is not diagonal dominant the system will not converse, so row exchanges is needed to make the matrix diagonally dominant before that operation is being set to operate.
_________________________________________________________________________________
Swapping rows & columns of matrix in MATLAB is a very simple task. Lets find out how.
Swapping MATRIX Rows in MATLAB
Suppose you are having a matrix A.
And you want, ith row to be exchanged with jth row. So the MATLAB command you will write is like: A([i j],:)=A([j i],:)
NOTE: Here ':' is indicating that we are considering all the columns of matrix A for this operation
Eg. if A=
1 2 3
3 4 5
5 6 7
You want row 2 & 3 to be exchanged, so you will write the command as,A([2 3],:)=A([3 2],:)
After this command operates on matrix A, the resultant matrix A will be,
A=
1 2 3
5 6 7
3 4 5
You want to swap any row with the last row, & you don't know at what indices value the last row belongs to, then you need to make the command like this
A([i end],:)=A([end i],:)
After this the ith row will be exchanged with the last row of the matrix A.
Swapping MATRIX Columns in MATLAB
Suppose you are having a matrix A.
And you want, ith column to be exchanged with jth column. So the MATLAB command you will write is like:
A(:,[i j])=A(:,[j i])
A(:,[i j])=A(:,[j i])
Eg. if A=
1 2 3
3 4 5
5 6 7
You want columns 2 & 3 to be exchanged, so you will write the command as,A(:,[2 3])=A(:,[3 2])
After this command operates on matrix A, the resultant matrix A will be,
A=
3 2 1
5 4 3
7 6 5
You
want to swap any column 'i' with the last column, & you don't know at what
indices value the last column belongs to, then you need to make the command
like this
A(:,[i end])=A(:,[end i])
After this the ith column will be exchanged with the last column of the matrix A.
0 comments: