Lecture 3

Matrix concatenations 

Using the brackets we can create more and more complicated matrixs:


>> [ones(2,2) zeros(2,2)]

>> [ones(2,2) ; zeros(2,2)]


Exercise

  • Create the matrix whose first column is the numbers 1:10, the next column contains the first 10 squares 1, 4, 9 ...100 and the third column contains the first 10 powers of 2: 2 4 8 16...1024.

More Expressions

All Matlab expressions are made out of basic building blocks put together: 

  • Constants
  • Variables
  • Operators
  • Keywords
  • Functions

 

Constants

We have met some constants already: 1, 3, 1.56, -5, pi. But there are more that we haven't yet met: i, j, (root of -1), 2.4e12 (2.4 times 10^12), 'a' (the character a), 'hello' (a vector consisting of the letters 'h','e','l','l','o').  Note that i and j can be used in a way different from variables: you can write 3i or -15j, but if you define a=10, writing 4a is an error... Also note that Matlab allow using the colon notation with letters: 'a':'z'. 

Note that you can access letters in a string just elements in any other vector: 

 

>> a='hello'   

>> a(2)

>> a(4:5)='p!' 

Exercise:

  • Create the string of letters 'zyx....cba'
  • Intertwine the two strings 'Hello!!' and 'Goodbye' (create the string 'HGeolold....!e')

Variables

There isn't much to say about variables. Variable names must start with a letter, and the variable name length must be no more than 32 characters. Names may use letters, numbers and the underscore '_'.

 

Note that matlab will make matrixs and vectors grow as needed (filling the empty spaces with zeros):

 

>> a = [1 2 ; 3 4]

>> a(5,5) = 1 


Operators

We have met many operators: =,+,-,/,*,^,.,',[,],(,),;,:. There are a few others, as we shall see shortly.

Keywords

We have not yet seen many keywords...the only exception is end. 

Functions

We have seen: ones, zeros, diag, size,...There are others. For example: sin, cos, exp, log, sqrt. You can also define new functions as we shall learn in the next lecture or two.

 

Plotting

Let's learn how to make nice pictures. plotting is one of the basic tools of Matlab. Most of the plotting happens through plot and its variants. Best is to learn by example:

 

>> x = 0:0.01:1 ;

>> y = sin(x) ;

>> plot(x,y)

 

Notice several things. 

First, the semicolon ';' at the end of the expression suppresses the output of the result. However, it does not inhibit the evaluation of the expression. So x and y are still as they would be if the semicolon was missing but the screen is blissfully clean (try it without the semicolon).

Second, notice that the plot function created a nice figure for us, with the first "wave" of the sin. try it put with other functions. 

Exercise:

  • Plot the sin over a different interval
  • Plot a more squiggily sin over the same interval
  • plot the function x^2 between the numbers -5 and 5
  • plot the function log(x) from 1 to 10 (be sure that your plots are nice and smooth)
  • plot a circle (think parametric plot)
  • plot a Lissajou curve
  • Plot a "heart" r = sin(theta/2)
  • Read more about plot and its optional arguments!


Note that using plot with only one input argument does something a little strange (but useful at times...).

Logical Constructs

Sometimes it is important to compare numbers. For example we might want to act differently if a given number is positive or negative. For this we need logical operators and comparisons. 

 

Testing

First, comparisons. we use the operators ==, ~=, < >, <=, >=  (the ~ key is usually at the top left of the keyboard, and it needs the SHIFT key) to compare numbers e.g:


>> 1==2

>> 3~=5

>> 5<=6

>> 7<=10


Notice that these expressions return 0 when they are false and 1 when the are true. This is the convention. 0 is false and 1 is true.

While this notation looks similar to math, there are a few pitfalls

  • 1<x<5 does NOT return what it does in math...in fact this will alway be true regardless of the value of x. Can you see why?
  • == is not =. Regardless of how many time I will say this, people will still make this mistake many times...but perhaps by saying it lots, it will happen less...maybe. remember...you have been warned...= is assignment, and == is a test for equality.


Boolean operations

What if we want to check two things? A and B? or perhaps A and (B or C)...for this we need Boolean operators: & (AND), | (OR), and ~(NOT)

(The | is usually found at the middle right of the keyboard, and it needs a SHIFT key.)

  • 1~=2 | 3<4
  • 3~=4 & 8==(4+4)
  • ~(2<=8) 


Formatting Text

Once in a while we would like to display some text that is nicely formatted and not just the output as Matlab wants to display. To do this we use the sprintf command.

In its simplest form it is quite straightforward...the first arguemnt is a format string, and the rest are parameters for the string. examples:

  • sprintf('hello, here is a number %d',17)
  • sprintf('my name is %s and I am %g years old','Yossi',31)
Read up on sprintf to get the whole picture.