How To Apply Double For Loop in MATLAB

Previously we discussed, How to apply FOR LOOP in MATLAB. Now in Post-8 of Module-1, we will discuss, how to apply 'double for loop' in matlab. 

For this problem, we are taking a example of matrix in which we are calculating its transpose. The screen capture of this matlab program with brief description of each line of code has been shown in figure-1.

Figure.1 - MATLAB Program for 'Double For Loop'.

The matlab program 'double for loop' consists of 11 lines of code. we will discuss about each line of code in details below:

Line-04 : Create a function named as ‘mod0108’ having single matrix of two dimension (2-D).
Line-05 : Calculate rows and columns in matrix A. Here r represent number of rows and c represents number of columns.
Line-06 : Start first ‘for loop’ from 1 to r.
Line-07 : Start second ‘for loop’ from 1 to c.
Line-08 : Interchange the element of matrix 'A' and place them in matrix 'At'.
Line-09 : End of second 'for loop'.
Line-10 : End of first 'for loop'.
Line-11 : Display static matter on command window as it is written in single quote.
Line-12 : Display matrix 'A' on command window.
Line-13 : Display static matter on command window as it is written in single quote.
Line-14 : Display matrix 'At' on command window.

To execute 'double for loop' matlab function, first of all create any matrix A. Example: A=[1 2 3 ; 4 5 6 ; 7 8 9]. Now call this matrix A using 'double for loop' matlab function on command window as shown in figure-2.

Figure.2 - Output from 'double for loop' matlab program.

The output generated using 'double for loop' matlab program clearly shows that matrix 'At' is transpose of the matrix 'A'. If you type function name after help command in command window, then it will display the matter written after %% in m-file.

Video Tutorial : How To Apply Double FOR Loop in MATLAB By INFO4EEE

Brought to you by INFO4EEE.

No comments

Powered by Blogger.