Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating arrays
Lecture 2
Variables are arrays of numbers. You name the variables (as the programmer) and assign them numerical values. You execute the assignment command to place the variable in the workspace memory (memory is part of hardware used for storing information). You are allowed to use the variable in algebraic expressions, etc. once it is assigned.
12oclockRock tertiarySector blue cows Eiffel65 red_bananas This_Variable_Name_Is_Quite_Possibly_Too_Lo ng_To_Be_Considered_Good_Practice_However _It_Will_Work % (the green part is not part of the recognized name)
There are different ways to name variables. The following illustrate some of the conventions used: lowerCamelCase UpperCamelCase underscore_convention If a variable is a constant, some programmers use all caps: CONSTANT It does not matter which convention you choose to work with; it is up to you.
Variables as Arrays
In MATLAB, a variable is stored as an array of numbers. When appropriate, it is interpreted as a scalar, vector or matrix.
scalar
11
vector
n 1 or 1 n
matrix
nm
The size of an array is specified by the number of rows and the number of columns in the array, with the number of rows indicated first.
Scalars
Scalars are 11 arrays. They contain a single value, for example:
Vectors
Matrices
Columns
1
1 2 3 4 3.0 4.6 0.0 2.3
2
1.8 -2.0 -6.1 0.3
3
3.6 21.3 12.8 -6.1
m = [3.0, 1.8, 3.6; 4.6, -2.0, 21.3; 0.0, -6.1, 12.8; 2.3, 0.3, -6.1]
Columns
1 1 2 3 4 3.0 4.6 0.0 2.3 2 1.8 -2.0 -6.1 0.3 3 3.6 21.3 12.8 -6.1
Hands-on
Vectors:
b = [1, 0, 2] c = [1 0 2]
Matrix:
d = [5, 4, 3; 0, 2, 8]
Hands-on
-7
whiteRabbit =
21 32
6 0
-5
-18.5
Scalar Operations
Operation
Addition Subtraction Multiplication Division Exponentiation
Algebraic Syntax
MATLAB Syntax
a+b a-b ab ab ab
a + b a b a .* b a ./ b a .^ b
Array Operations
Arrays of numbers in MATLAB can be interpreted as vectors and matrices if vector or matrix algebra is to be applied. Recall that matrices are mathematical objects that can be multiplied by the rules of matrices. To do matrix multiplication, you need to use the standard *, /, and ^ operators [without the preceding . (dot)]. They are not for array multiplication, division and exponentiation. To deal with arrays on an element-by-element level we need to use the following array or dot-operators:
.*
./
and
.^
./
and
.^
Because scalars are equivalent to a 11 array, you can either use the standard or the dot-operators when doing multiplication, division and exponentiation of scalars (i.e., of single numbers). It is okay for you to always use the dotoperators, unless you intend to perform vector or matrix multiplication or division.
Example:
x = [2, 1; 3, 4] y = [5, 6; 7, 8]
z = x .* y results in [10, 6; 21, 32]; this is array multiplication z = x * y results in [17, 20; 43, 50]; this is matrix multiplication So, do NOT forget the dot when doing array operations! (.* ./ .^)
Hierarchy of Operations
Just like in mathematics the operations are done in the following order: Left to right doing what is in Parentheses & Exponents first, followed by Multiplication & Division, and then Addition & Subtraction last.
An example:
c c c c c
= = = = =
c c c c c
= = = = =
Hands-on
Multiply, element-by-element, a b.
Since this is an array operation, the .* multiplication operation is implied by the request.
Remark: vectors and matrices are special mathematical objects, arrays are lists or tables of numbers.
The equal sign (=) commands that the number computed on the right of it is input to the variable named on the left; thus, it is an assignment operation.
-3 2 2 6
-3 2 2 6
12 2 1 0
18 5 1 -2
-3 2 2 6
0 0 0
0 0 0
0 0 0
1 1
Note: Placing a single number inside either function will return an n n array. e.g. ones(4) will return a 4 4 array filled with ones.
Index a number used to identify elements in an array Retrieving a value from an array:
G = [1, 2, 3; 4, 5, 6; 7, 8, 9] G = 1 2 3 4 5 6 7 8 9
G(2,1) ans = 4 G(3,2) ans = 8
A(2) = 5 A = 12
-3
A(6) = 8 A = 12
-3
Notice how undefined values of the array are filled with zeros
Colon Operator
Colon notation can be used to define evenly spaced vectors in the form:
first : last
H = 1:6 H = 1
I = 1:2:11 I = 1 3
11
The colon represents an entire row or column when used in as an array index in place of a particular number.
G = 1 4 7 2 5 8 3 6 9
G(:,1) ans = 1 4 7
G(:,3) ans = 3 6 9
G(2,:) ans = 4
The colon operator can also be used to extract a range of rows or columns:
G = 1 4 7 2 5 8 3 6 9
G(2:3,:) G = 4 7
5 8
6 9
G(1,2:3) ans = 2
Manipulating Arrays
The transpose operator, an apostrophe, changes all of an arrays rows to columns and columns to rows.
J = [1 , 3, 7] J = 1 3
J' ans =
7 1 3 7
The functions fliplr() and flipud() flip a matrix left-to-right and top-to-bottom, respectively.
Experiment with these functions to see how they work.
Hands-on exercise:
The first row ranges from 1 to 5 in increments of 1 The second row ranges from 10 to 18 in increments of 2
Hands-on continued
Transpose this matrix and assign it to variable Y. >> Y = x Extract the 2nd row from Y and assign it to variable Z. >> Z = Y(2,:)
Summary (1 of 2)
Naming a variable: Start with letter followed by any combination of letters, numbers and underscores (up to 63 of these objects are recognized). Arrays are rows and columns of numbers. Array operations (element-by-element operations with the dot-operators) Hierarchy of arithmetic operations.
Summary (2 of 2)
Command lines that assign variables numerical values start with the variable name followed by = and then the defining expression An array of numbers is the structure of variables in MATLAB. Within one variable name, a set of numbers can be stored. Array, vector and matrix operations are efficient MATLAB computational tools.