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])

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:

Check Diagonal dominance Row Wise & Column Wise using MATLAB

Diagonal Dominance Row Wise

If a Matrix is diagonal dominant row wise, the absolute value of the diagonal element in that matrix must me greater than or equal to the sum of absolute value of other elements in that row of the matrix. 

In MATLAB terms, abs(diag(A)) must be greater than sum(abs(A),2)-abs(diag(A)). The abs(diag(A)) is subtracted from sum(abs(A),2) since the command also include the diagonal element in the sum done row wise.
Thus the net formula to check the row dominance row wise can be seen as abs(diag(A))-(sum(abs(A),2)-abs(diag(A)))==2*abs(diag(A))-sum(abs(A),2) which must result greater than zero in order to have a dominance. The condition has to be true for all the rows of the matrix.

Thus the MATLAB command which will help to find dominance will be;

x=all((2*abs(diag(A)))- sum(abs(A),2)>=0); %sum(abs(A),2) will perform addition row wise
if x==0
disp('Matrix is not Diagonal Dominant Row wise')
else
disp('Matrix is Diagonal Dominant Row wise')
end

Where A is a square matrix. Variable x stores the logical value of either 1 or 0 depending on the Matrix A specified by the user.
x will have a value of 0 if the matrix A is not diagonal dominant row wise.
x will have a value of 1 if the matrix A is diagonal dominant row wise.


Sample Input & Output:
(1) if  

A =[1     2     3
     4     5     6
     7     8     9]

x =     0

Matrix is not Diagonal Dominant Row wise


(2) 
if 
B =[4     1     1
     4     9     2
     2     3     8]
x =    1
Matrix is Diagonal Dominant Row wise


Diagonal Dominance Column Wise

In this case all the other conditions are same, only sum(abs(A),2)  is changed to sum(abs(A)) or sum(abs(A),1) (Here 1 is the default argument of the  function if the value is not specified)

The the MATLAB program to Check for the Diagonal Dominance Column Wise:

x=all((2*abs(diag(A)))- sum(abs(A))>=0);
%sum(abs(A)) will perform addition column wise
if x==0
disp('Matrix is not Diagonal Dominant Column wise')
else
disp('Matrix is Diagonal Dominant Column wise')
end

Where A is a square matrix. Variable x stores the logical value of either 1 or 0 depending on the Matrix A specified by the user.
x will have a value of 0 if the matrix A is not diagonal dominant column wise.
x will have a value of 1 if the matrix A is diagonal dominant column wise.

NOTE : One more variant of the code for Diagonal Dominance Column Wise could be that in the initial step we take a transpose of the matrix A. Then using the transposed value in the subsequent steps.

Sample Input & Output:
(1) if  

A =[1     2     3
     4     5     6
     7     8     9]

x =     0

Matrix is not Diagonal Dominant Column wise


(2) 
if 
B =[8     1     1
     4     9     2
     2     3     8]
x =    1
Matrix is Diagonal Dominant Column wise


Further Variation of this program  may include functions or options to check for Diagonal Dominance column & row wise simultaneously.

Update 03/09/2014
To check for the condition of strict diagonal dominance. The condition would be that instead of ">=" sign we should be using only ">" sign.
i.e.,
To check for the matrix to be strictly diagonal dominant row wise the MATLAB code snippet will become as,

x=all((2*abs(diag(A)))- sum(abs(A),2)>0); %sum(abs(A),2) will perform addition row wise
if x==0
disp('Matrix is not Diagonal Dominant Row wise')
else
disp('Matrix is Diagonal Dominant Row wise')
end 

Same thing for matrix to be strictly diagonal dominant column wise the MATLAB code snippet will be,

x=all((2*abs(diag(A)))- sum(abs(A))>0); %sum(abs(A)) will perform addition row wise
if x==0
disp('Matrix is not Diagonal Dominant Row wise')
else
disp('Matrix is Diagonal Dominant Row wise')
end

0 comments: