Unit# 9- MATLAB 2 | |||||||||||||
|
|||||||||||||
|
|||||||||||||
In MATLAB we will use the while and for loops. They are very similar to the ones we learned in C++. The following tables illustrate the similarities/differences:
The following pictures illustrate a complete program in MATLAB which utilizes the above segment. The condition is slightly different to make sure the program does not fall into an infinite loop.
The results are shown here:
In the while structure, another difference between C++ and MATLAB is that the condition of while may be actually a condition on arrays. The following example illustrates this in a very simple way:
we see that as soon one of the components of the array evaluates to false, the loop terminates. -------------------------------------------------------------------------------------------------------------------------------------------- We can now study the for structure. The following table illustrates the similarities/differences:
The following pictures show the complete .m file and the results of running it in MATLAB.
Again in the for structure, another difference between C++ and MATLAB is that the for index may be actually set equal to a matrix. Then the loop starts by using as index the first column of the array, then the second column etc. until the last column is used. The following example illustrates this in a very simple way:
|
|||||||||||||
![]() |
|||||||||||||
Jacob
Y. Kazakia © 2001 All rights reserved |