Previously we discussed, How to apply 'Double For Loop' in MATLAB. Now in Post-9 of Module-1, we will discuss, how to interchange a single element of two matrices in matlab.

To understand this problem, we are interchanging second diagonal element of two 3x3 matrices. The screen capture of matlab program with brief description about this has been shown in figure - 1.

Figure.1 - MATLAB Program for interchange of single diagonal element of two matrices.

Now we will give you detailed explanation line by line of matlab program in following section:

Line-04 : Load 3x3 matrix in A.
Line-05 : Load 3x3 matrix in B.
Line-06 : Display message written in single quote.
Line-07 : Display matrix A before element interchange operation.
Line-08 : Display matrix B before element interchange operation.
Line-09 : Copy second diagonal element of matrix A into temporary variable.
Line-10 : Replace second diagonal element of matrix A by second diagonal element of matrix B.
Line-11 : Replace second diagonal element of matrix B by element stored in temporary variable.
Line-12 : Display message written in single quote.
Line-13 : Display matrix A after element interchange operation.
Line-14 : Display matrix B after element interchange operation.

Now run this matlab program by clicking on 'Run Mod0109.m' button OR you can run this program directly by writing its name on command window. The screen capture of command window has been shown in figure - 2.

Figure.2 - Result of element interchange matlab program.

Result clearly shows that second diagonal element of both the matrix has been interchanged. You can select element which you want to interchange by changing the value of row and column written in small bracket of matrix. For example, if you want to interchange third element of first row you need to write A(1,3).

Video Tutorial : How To Interchange a Single Element of Two Matrix in MATLAB

Brought to you By INFO4EEE.