SUNY Geneseo Department of Mathematics
Monday, March 9
Math 230 02
Spring 2015
Prof. Doug Baldwin
% print every number between 10 and 20
>> for i = 10:20
fprintf( 'The current number is %d\n', i )
end
The current number is 10
The current number is 11
The current number is 12
The current number is 13
The current number is 14
The current number is 15
The current number is 16
The current number is 17
The current number is 18
The current number is 19
The current number is 20
for i = first : last
...
calculate with i
...
end
for i = 1:length(v) ... calculate with v(iv) ... end
orfor i = v ... calculate with i ... end
% Print all elements of M
>> M = [ 12 48; 72 17 ]
M =
12 48
72 17
>> for i =1:2
for j = 1:2
fprintf('The current value = %f\n', M(i,j) );
end
end
The current value = 12.000000
The current value = 48.000000
The current value = 72.000000
The current value = 17.000000
for i = 1:rows
for j = 1:columns
...
calculate with M(i,j)
...
end
end
% Add all elements in v
>> sum = 0;
>> for i = 1:length(v)
sum = sum + v(i);
end
>> v
v =
17.0000 9.0000 -4.0000 3.1416 9.2000
>> sum
sum =
34.3416
initialize total
for i = v
...
total = f( total, i )
...
end