matlab5 - Loops II For Loop For <index variable> =...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Loops II
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
For Loop For <index variable> = <vector of values> execute statements for each value of index end for x = [1 4 7 9] disp(num2str(x^2)); end 1 16 49 81
Background image of page 2
Drawing a Line for cX = [10 11 12 13 14 15 16 17 18 19 20] putPixel(cX, 10, g) end a better way? for cX = 10:1:20 putPixel(cX, 10, g) end for cX = 10:20 putPixel(cX, 10, g) end
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Adding numbers 1-5 i = 1; sum = 0; while ( i <= 5) sum = sum + i i = i + 1; end sum = 0; for i = 1:5 sum = sum + i end using for loop ?
Background image of page 4
Adding Elements of a Vector Given vector v, sum up its contents sum = 0; for i = 1:length(v) sum = sum + v(i) end sum = 0; i = 1; while ( i <= length(v) ) sum = sum + v(i) i = i + 1; end
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Minimum Element of a Vector min = inf; for i = 1:length(v) if (v(i) < min) min = v(i) end end What if we need the location as well? min = inf; index = 0; for i = 1:length(v) if (v(i) < min) min = v(i) index = i end end
Background image of page 6
Drawing a Rectangle putPixel(10, 10, g); putPixel(11, 10, g); putPixel(12, 10, g); putPixel(10, 11, g); putPixel(11, 11, g);
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/29/2010 for the course CS 111 taught by Professor Stuff during the Fall '08 term at Bilkent University.

Page1 / 21

matlab5 - Loops II For Loop For &lt;index variable&gt; =...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online