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

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

Engineering 101 Engineering 101 Lecture 35 Lecture 35 3D Data Representation 3D Data Representation Prof. Michael Falk University of Michigan, College of Engineering

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

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

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

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

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

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

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

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

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

View Full Document
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
Exercise 1 Exercise 1 Which function subtracts 1 from every number that is divisible by s in matrix A?

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

View Full Document
Exercise 1 Exercise 1 Which function subtracts 1 from every number that is divisible by s in matrix A?
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
Ask a homework question - tutors are online