Lecture 3Matrix concatenationsUsing the brackets we can create more and more complicated matrixs: >> [ones(2,2) zeros(2,2)] >> [ones(2,2) ; zeros(2,2)] Exercise
More ExpressionsAll Matlab expressions are made out of basic building blocks put together:
ConstantsWe 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:
VariablesThere 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 OperatorsWe have met many operators: =,+,-,/,*,^,.,',[,],(,),;,:. There are a few others, as we shall see shortly. KeywordsWe have not yet seen many keywords...the only exception is end. FunctionsWe 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.
PlottingLet'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:
Logical ConstructsSometimes 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.
TestingFirst, 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
Boolean operationsWhat 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.)
Formatting TextOnce 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:
|