{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# matlab5 - Loops II For Loop For =

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

Loops II

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

View Full Document
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
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

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

View Full Document
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 ?
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

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

View Full Document
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
Drawing a Rectangle putPixel(10, 10, g); putPixel(11, 10, g); putPixel(12, 10, g); putPixel(10, 11, g); putPixel(11, 11, g);

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 21

matlab5 - Loops II For Loop For =

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

View Full Document
Ask a homework question - tutors are online