Reputation: 29
This is from my instructor. I need help with the particular coding below involving iterating through the rows and of the columns in a previously determined array. colNum = 20 and rowNum 10. Also the tempToColor function is already defined and working and it creates a 101x3 matrix each row giving a different color shade according to a temperature between 0-100 degrees.
"This function has a finElems parameter and doesn't return anything. It plots all the temperature values found in the finElems array. You can see my example plot below. To plot a color, use a statement like this:
plot(colNum, numRows-rowNum, 's', 'Color', color, 'MarkerFaceColor', color, 'MarkerSize', 20);
In order to use this statement you should create a few variables:colNum:This is the current column number .You will iterate through all the columns in the array, so store the current column number in this variable. rowNum: Same as colNum, except it's the current row number. •color:This is the color of the current finElems element as determined by the function tempToColor .I found it necessary to use the expression numRows - rowNum so that the bottom row of the array (the highest numbered row) appears as the bottom row on the graph. Otherwise the fin appears upside down in the graph. Use this outline for the function:
function plotFin(finElems)
hold on;
% iterate through all the rows:
% iterate through all the columns: (this is a loop within a loop)
% Use the tempToColor function to get the temperature.
% Plot the temperature color.
% end
% end
axis equal tight;
axis([0 (numCols+1) -1 numRows]);
hold off;
end
Upvotes: 2
Views: 22999
Reputation:
Some of Matlab's "tools" for iteration are the loop statements. If you know before iterating how many times you'll execute the loop, then for
is your thing. If you don't know, and you need to decide every time if you'll continue to iterate or not, then you may use while
. If you have Matlab, open it, and in the editor window write for
and while
, then go with the cursor on each of them and press F1. (Don't worry, the text will be here until you return).
In your case, you know beforehand how many rows (10) and how many columns (20) you have to iterate trough. So, what you need is?... that's right, you need a for
. In the rows case the statement might look like this:
for num_rows = 1:10
% Here add some code
end;
This tells Matlab to execute whatever is inside ten times. Each time, the value of num_rows
will be different inside the for
loop's body: first time is 1, then 2, then 3 and so on up to 10.
Now, the plan is: for each row, you have to iterate trough columns, also. For this, one may combine two loops one into another (I think the word is "nest" --- sorry, not a native English speaker. Think of nested loops like the numbers on an electronic watch: for each hourly iteration there are 60 minute-iterations.) The code for this may look like:
for num_rows = 1:10
for num_cols = 1:20
% Here some other code
end;
end;
Now, the num_cols
value for each inner iteration goes from 1 to 20 when num_rows
is 1, then goes again from 1 to 20 when num_rows
is 2, and so on until num_rows
is 10 and num_cols
is 20.
With this stuff clarified (I hope), I recommend for you to go and press F1 on more stuff in Matlab; it is among the best-written help docs out there, and would be a pity not to take advantage of it.
Upvotes: 3