You are on page 1of 2

Matrices and Operators

Pi is not a built-in variable, it’s a built-in function. Instead of doing something, it just returns a double
value. Oh, and functions without parenthesis were called commands in the previous notes, they are still
functions, not commands.

Sin() is the function that operates on radians. To get degrees, use sind().

Size function gives the dimensions of a matrix.

It’s a common practice to use small letters to express scalars and vectors, but capital letters for matrices.
Scalars are 1x1 vector, aka just individual numbers

The Colon Operator

An operator is a function that’s invoked by a symbol.

A function is an operation that’s invoked by a name.

To express range- start:increment:end

+ operator and the plus() function does the same thing.

There’s also a function named colon(), that takes start, increment and end as it’s arguments.

You always get a row vector with a colon operator.

If for example you input 7:3:1, it’s not possible, you’ll get an empty matrix.

You can choose to not specify the increment, in that case matlab will use default increment (1).

Accessing Parts of a Matrix

Indexing: The name of the matrix will work as a function, as arguments, you’ll need to give the row and
the column.

You can use the same notation to assign values to specific positions on the matrix i.e., X(1,2) = 4;

If you try to assign values to a non-existent matrix or non-existent position of a matrix, the matrix will
either be created or extended minimally to fit the assignment and the adjacent positions will be filled
with 0.

Subarrays: Like the previous notation instead of passing scalars you can use vectors or matrices of
positions as arguments as long as the argument matrix elements do not exceed the positions of the X
matrix. And yes, you can go crazy with colon operators when specifying argument matrices.

The end keyword if used as row argument, will return the end row position of the matrix, like X(end,2).
For column, well you can guess it.

End is a keyword, pi is not. You can use pi as a variable name, but not end.

You can use X(:,2) or X(1,:) which will select the entire row or column. Guess what X(:, :) will do.
You can even assign arrays to subarrays, but they must match exactly.

Combining and Transforming Matrices

You can concatenate subarrays to build bigger arrays as long as their dimensions are consistent.

In other words, you can use matrices instead of numbers when defining a matrix.

To transpose a matrix use ‘ after the matrix.

‘ operator is of higher rank than other operators, so it goes first. 1:3’ will not give column vector. To get
column vector, use (1:3)’.

Arithmetic

(You better watch the second video again)

Array addition, subtraction, multiplication, division will work only if the dimensions agree of the
operands. Respective operators are +, -, .*, ./ or .\

Individual elements of the array will be operated on to produce final result.

Matrix multiplication is different, it involves multiplication and addition. The operator used is *. But you
already know in detail about matrix multiplication. Application of matrix multiplication can be
calculating overall gains of a group of people with some stocks. The new concept here is that the “Inner
Dimensions :# of columns of left matrix and # of rows of right matrix” must be equal for matrix
multiplication.

Exponentiation: for elementwise use .^ If you use i.e., X^2, it means X*X. So, X needs to be square
matrix. The power must be scalar.

Operations with scalars: if one operand is scalar and the other is a matrix, each element of the matrix is
operated with the scalar. Elementwise and overall operators work differently here, for example, 2/X or
X/3 won’t work since dimensions do not agree. But 2*X and 2.*X works the same way. Better watch the
second video again for clarification.

You might also like