What does C store?

40
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) 1 1 1 1 1 1 a) 1 2 3 1 1 1 d) 1 2 3 1 1 1 b) 1 2 3 1 2 3

description

What does C store?. >>A = [1 2 3] >>B = [ 1 1 ] >>[C,D]= meshgrid (A,B). b ) 1 2 3 1 2 3. a) 1 2 3 1 1 1. 1 2 3 1 1 1. 1 1 1 1 1 1. What does D store?. >>A = [1 2 3] >>B = [ 1 1 ] >>[C,D]= meshgrid (A,B). b ) 1 2 3 1 2 3. - PowerPoint PPT Presentation

Transcript of What does C store?

Page 1: What does C store?

What does C store?

>>A = [1 2 3]>>B = [1 1]>>[C,D]=meshgrid(A,B)

c) 1 1 1 1 1 1

a) 1 2 3 1 1 1

d) 1 2 3 1 1 1

b) 1 2 3 1 2 3

Page 2: What does C store?

What does D store?

>>A = [1 2 3]>>B = [1 1]>>[C,D]=meshgrid(A,B)

c) 1 1 1 1 1 1

a) 1 2 3 1 1 1

d) 1 2 3 1 1 1

b) 1 2 3 1 2 3

Page 3: What does C store?

What does C store?

>>A = [1 2 3]>>B = [1 1]>>[C,D]=meshgrid(B,A)

c) 1 2 3 1 2 3

a) 1 1 1 1 1 1 1 1 1

d) 1 1 2 2 3 3

b) 1 1 1 1 1 1

Page 4: What does C store?

What does D store?

>>A = [1 2 3]>>B = [1 1]>>[C,D]=meshgrid(B,A)

c) 1 2 3 1 2 3

a) 1 1 1 1 1 1 1 1 1

d) 1 1 2 2 3 3

b) 1 1 1 1 1 1

Page 5: What does C store?

LEARNING ABOUT FUNCTIONS IN MATLAB

Page 6: What does C store?

A simple function (poly2)

Save the file as poly2.m (same as the name of the function)

Page 7: What does C store?

The function is available from the command window or from other M-file programs

Page 8: What does C store?

Comments

You should comment functions, just as you would any computer codeThe comment lines immediately after the first line are returned when you query the help function

Page 9: What does C store?

Functions with Multiple Inputs

A user defined function with multiple inputs

Page 10: What does C store?

Functions with Multiple Outputs

This function returns 3 output values

If you don’t ask for all three results, the program just returns the first value

Page 11: What does C store?

User Defined Input

To this point we have “hard coded” the values of variables into our M-file programsThe input function allows us to prompt the user to enter a value

Page 12: What does C store?

The input function is used in an M-file program to prompt the user to enter a value

The prompt is displayed in the command window

Page 13: What does C store?
Page 14: What does C store?

Input also can input in matrices and characters

Page 15: What does C store?

Output Options

Use the disp functionUse the fprintf function – same as C except single quotes

Page 16: What does C store?

disp

The display (disp) function can be used to display the contents of a matrix without printing the matrix name

Page 17: What does C store?

The disp function can also be used to display a string

Page 18: What does C store?

You can combine disp functions to create meaningful output from an M-file program, but the result of each disp function is on a separate line.

Page 19: What does C store?

Using find and logical operators in Matlab

Page 20: What does C store?

Logical Operators

& and~ not| or

Page 21: What does C store?

find

The find command searches a matrix and identifies which elements in that matrix meet a given criteria.

Page 22: What does C store?

index numbers

element values

Page 23: What does C store?

find used with a 2D matrix

x =[1 2 3; 10 5 1; 12 3 2; 8 3 1] element = find(x > 9) [row, column] = find(x > 9)

Returns a single element number

Returns the row and column designation of an element

Page 24: What does C store?

The rest of Chapter 5 shows how to use if/else if and for loops in Matlab – very similar to C

Page 25: What does C store?

Homework – Chapter 5, problems 1 and 2

These are a little tougher – so good luck!

Page 26: What does C store?

Simple if

if comparison statementsend

For example….

if G<50 count = count +1; disp(G);end

Page 27: What does C store?

If statementsEasy to interpret for scalarsWhat does an if statement mean if the comparison includes a matrix? The comparison is only true if it is true

for every member of the array

G=[30,55,10]if G<50

count = count +1; disp(G);end

The code inside the if statement is not executed, because the comparison is not true!!

Page 28: What does C store?

The if/else structure

The simple if triggers the execution of a block of code if a condition is trueIf it is false that block of code is skipped, and the program continues without doing anythingWhat if instead you want to execute an alternate set of code if the condition is false?

Page 29: What does C store?

Use an if structure to calculate a natural log

Check to see if the input is positive If it is, calculate the natural log If it isn’t, send an error message to the

screen

Page 30: What does C store?

The if/else/elseif structureUse the elseif for multiple selection criteriaFor example Write a program to determine if an

applicant is eligible to drive

Page 31: What does C store?

Repetition Structures - Loops

Loops are used when you need to repeat a set of instructions multiple times MATLAB supports two types of loops for while

Page 32: What does C store?

When to use loops

In general loops are best used with scalarsMany of the problems you may want to attempt with loops can be better solved by vectorizing your code or with MATLAB’s logical functions such as find

Page 33: What does C store?

For Loops

for index = [matrix]commands to be executed

end

• The loop starts with a for statement, and ends with the word end.

• The first line in the loop defines the number of times the loops will repeat, using an index number. The loop is executed once for each element of the index matrix identified in the first line

• The index of a for loop must be a variable.

Page 34: What does C store?

Here’s a simple example

In this case k is the index – the loop is repeated once for each value of k

the index can be defined using any of the techniques we’ve learned

Page 35: What does C store?

Here’s a simple example

In this case k is the index – the loop is repeated once for each value of k

the index can be defined using any of the techniques we’ve learned

Page 36: What does C store?

While Loops

while criterion commands to be executedend

While loops are very similar to for loops. The big difference is the way MATLAB decides how many times to repeat the loop. While loops continue until some criterion is met.

Page 37: What does C store?

This loop creates the matrix a, one element at a time

Page 38: What does C store?

Improving the Efficiency of Loops

In general, using a for loop (or a while loop) is less efficient in MATLAB than using array operations.

Page 39: What does C store?

The amount of time it takes to run this code will depend on your computer

Here’s an example. This code creates a 40,000 element matrix of ones, then multiplies each element in the matrix by pi

These two lines of code start a timer to measure the elapsed time required to run the lines of MATLAB code between them

Page 40: What does C store?

This code accomplishes the same thing with a for loop