An interactive introduction to MATLAB

(Jeff_L) #1
1.4variables and arrays 9

Listing 1.10: Creating vectors using ranges
1 >> z = 8:1:
2 z =
3 8 9 10

5 >> v = linspace(0,10,5)
6 v =
7 0 2.5000 5.0000 7.5000 10.

Comments:


  • A range can be created using the colon operator, e. g.8:1:10means
    create a range that starts at 8 and goes up in steps of size 1 until 10.

  • A range can also be created using thelinspacefunction,
    e. g.linspace(0,10,5)means create a range between 0 and 10 with 5
    linearly spaced elements.


clear and clc commands
Theclearcommand can be used if you want to clear the current workspace of
all variables. Additionally, theclccommand can be used to clear the Command
Window, i.e. remove all text.

Variables and simple arrays


(http://www.eng.ed.ac.uk/teaching/courses/matlab/unit01/variables-
arrays.shtml)
MATLABexcels at matrix operations, and consequently the arithmetic
operators such as multiplication (), division (/), and exponentiation (^)
perform matrix multiplication, division, and exponentiation, when used on a
vector, by default. To perform an element-by-element multiplication, division,
or exponentiation you must precede the operator with a dot. Table 2 and
Listing 1.11 demonstrate the dot operator.
Listing 1.11: The dot operator
1 >> clear
2 >> a = [2 3; 5 1]
3 a =
4 2 3
5 5 1
6 >> b = [4 7; 9 6]
7 b =
8 4 7
9 9 6
10 >> a
b
11 ans =

Free download pdf