MATLAB Week 6
This week, I decided to focus on the for loop. The for loop has
special features. The for loop repeats a specified number of times. For
example, above the ee = 3:6 states to go from 3 through 6 in increments of one.
X(ee) extracts the term corresponding to the ee value from the row vector (if
ee was 3 it extracts the 3rd term from the row vector, if ee was 4
it extracts the fourth term from the row vector, etc…). The for loop repeats
this process until ee runs out of values (in this case after ee = 6). Regarding
the English counterparts, this makes sense; read it as the English language. For
ee = 3 will equal 3*2 and so on. Below is a table that goes over each induvial
step.
|
Step Number |
ee |
X(ee) |
|
1 |
3 |
[0 0 6] |
|
2 |
4 |
[0 0 6 8] |
|
3 |
5 |
[0 0 6 8 10] |
|
4 |
6 |
[0 0 6 8 10 12] |
The nested loop function is the same, except two for loops
exist. An outer loop and inner loop exist. The outer loop is the first for
loop. The inner loop is the second for loop. MATLAB recognizes the first outer
loop and realizes that it must repeat a specified number of time (in this case
the first for loop goes from 1 through 4). In addition, MATLAB recognizes the
second for loop and realizes that it must repeat a specified number of times
(here, bb depends on aa). MATLAB completes the second loop before going back to
the first loop. Above is an example. Firstly, fact =zeros(1,4) means that a
matrix of only zeros ranging from 1 row to 4 columns generates ([0 0 0 0] AKA row
vector). Secondly, we defined aa as going from 1 through 4 meaning that aa will
repeat 4 times, a specified number of times (aa = 1, aa= 2, aa=3, and aa=4). Thirdly,
P will always equal one following aa. Fourthly, bb will go from 1 through aa
repeating aa number of times, a specified number of times (bb = 1:1, bb = 1:2,
bb = 1:3, and bb = 1:4). Remember, bb ends when the specified number of steps
end. If bb=1:1 it will end in 1 step and if bb=1:2, it will end in 2 steps. It
then multiplies bb by P(P will always be 1) and creates a new value for P. At
each of the for steps, bb must complete its own loop before ending, where
MATLAB continues the first loop if more steps are necessary. Lastly, after
MATLAB completes the bb for loop, it extracts the number aa term, from the
previously defined fact row vector, and sets it equal to P. Below is a table
with the values at each step. I will not include the step number since the
inner and outer loops make it unclear on separating into steps. Also, the
spaces in aa and Fact are there because bb is in the process of repeating and
creating new values while aa and Fact are held fixed waiting for bb to complete
its loop.
|
aa |
bb |
P |
Fact |
|
Not defined yet |
Not defined yet |
Not defined yet |
[0 0 0 0] |
|
1 |
1 |
1 |
[1 0 0 0] |
|
2 |
1 |
1 |
|
|
|
2 |
2 |
[1 2 0 0] |
|
3 |
1 |
1 |
|
|
|
2 |
2 |
|
|
|
3 |
6 |
[1 2 6 0] |
|
4 |
1 |
1 |
|
|
|
2 |
2 |
|
|
|
3 |
6 |
|
|
|
4 |
24 |
[1 2 6 24] |
Application (multiplication
table)
An application of the for-loop function is creating a
multiplication table with a user defined number of rows and columns. The input function
allows the user to choose how many rows and columns they want to display. Here,
I chose 9. Using the same rules and logic as previously described, MATLAB
creates a multiplication table as you can see in the image above.
Comments
Post a Comment