Lecture35 - Engineering 101 Lecture 35 3D Data...

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

View Full Document Right Arrow Icon
Engineering 101 Engineering 101 Lecture 35 Lecture 35 3D Data Representation 3D Data Representation Prof. Michael Falk University of Michigan, College of Engineering
Background image of page 1

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

View Full Document Right Arrow Icon
Announcements Announcements Project 8 due Weds, 12/6 at 9pm Written work due Thurs/Fri 12/7-8 in your lab section. Because of a grading discrepancy Exam 3 will be handed back next week Tues/Weds. Gradebook will be updated Tuesday.
Background image of page 2
Vectorizing Vectorizing It is possible to use for loops to perform mathematical operations on arrays. But taking advantage of MATLAB’s natural ability to work with arrays and vectors greatly speeds up code. These expressions are almost always faster than for or while loops
Background image of page 3

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

View Full Document Right Arrow Icon
A logical array can be used to determine when an operation will be carried out. Suppose we wanted to subtract 2 from all values greater than 5 x = [ 1 7 10; 9 4 2 ]; y = x > 5; x(y) = x(y)-2; x= 1 5 8 7 4 2 1 7 10 9 4 2 Vectorizing Expressions Vectorizing Expressions y= 0 1 1 1 0 0
Background image of page 4
Vectorizing Expressions Vectorizing Expressions This method can also be used to avoid if else expressions What if we want to create a vector f that has the values of sin(t) if sin(t) is positive otherwise f should be 0? x = 0:pi/50.0:2.0*pi; f = sin(x); select = f < 0; f(select) = 0; plot(x, f);
Background image of page 5

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

View Full Document Right Arrow Icon
Example 1 Example 1 Which is an equivalent vectorized expression? for i = 1:100 a(i) = exp(-0.2 * i); end i = 1:100; a = exp(-0.2 .* i );
Background image of page 6
Example 2 Example 2 Which is an equivalent vectorized expression? % rand fills a matrix with random numbers % otherwise the syntax is like zeros g=rand(1, 10); for i = 1:2:length(g)-1 g(i) = g(i+1); end g=rand(1,10); g(1:2:end) = g(2:2:end);
Background image of page 7

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

View Full Document Right Arrow Icon
Example 3 Example 3 Which loop does the same thing as a = 1: 5; j = [ a ; a ; a ; a; a ]; i = j’; x = (i-1).*5 + j; x = zeros(5) for i= 1:5 for j=1:5 x(i,j) = (i-1)*5+j; end end
Background image of page 8
Which loop does the same thing as a = 1: 5; j = [ a ; a ; a ; a; a ]; i = j’; x = (i-1).*5 + j; x = zeros(5) for i= 1:5 for j=1:5 x(i,j) = (i-1)*5+j; end end Example 3 Example 3 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1 5 5 5 5 5 4 4 4 4 4 3 3 3 3 3 2 2 2 2 2 1 1 1 1 1 a j i
Background image of page 9

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

View Full Document Right Arrow Icon
Example 4 Example 4 Which loop does the same as: sel = (y>2); y(sel) = y(sel) / 2; for i = 1:size(y, 1) for j = 1:size(y, 2) if ( y(i,j) > 2 ) y(i,j) = y(i,j)/2; end end end
Background image of page 10
Exercise 1 Exercise 1 Which function subtracts 1 from every number that is divisible by s in matrix A?
Background image of page 11

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

View Full Document Right Arrow Icon
Exercise 1 Exercise 1 Which function subtracts 1 from every number that is divisible by s in matrix A?
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 47

Lecture35 - Engineering 101 Lecture 35 3D Data...

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

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