MATLAB Basics Review


Variable assignment

varName = expression

How assignment works

LHS    =    RHS

 

Naming rules

can't be a keyword

 

Note: MATLAB does syntax highlighting

 

Naming conventions

be descriptive

don't use a name already in use

 

 


What vector does each of the following create?

a = 2 : 7


b = [ 1 : 2 : 11 ]


c = -4 : 3 : 10 



d = 10 : 2 : 1 



e = [ c , a ]




Trace the execution of the following code

vec = [ 5 2 1 6 3 ]


vec(1, 3) = 4


vec(8) = 9


vec(2:4) = -1

Change the values at the even indices vec to 7

 

Determine how long a vector is (i.e., how many elements it has)

 

 


What matrices are created by the following?

mat = [ 11:15 ; 21:25 ; 31:35 ]




mat( 1, : )


mat( [ 1 2 ], 3 )


mat( [ 2 3 1 ], [ 4 1 5 ] )



All the rows and the odd-numbered columns?

How many rows and columns?

 

 


Scripts

script = stored sequence of MATLAB commands, also called a program or an M-file

Suppose we've written a script and saved it in tester.m. To run it from the Command Window:

>>