Array and Matrix Operations

Created


Types of Arrays

MATLAB collects all data into arrays (collection of data values organized in rows and columns)

Types of arrays: Scalar is an array, a single data value, with only 1 row and 1 column. a=3a=3 Vector is an 1D array, like a list. It is an ordered collection of numbers. b=[123], c=[123]b=\begin{bmatrix}1&2&3\end{bmatrix},\, c=\begin{bmatrix}1\\2\\3\end{bmatrix} Matrix has more than 1D, like a table. d=[12.263.115]d=\begin{bmatrix}1&2.2&6\\3.1&1&5\end{bmatrix}

Vectors

When you typically hear "x is a vector" in math books, they mean one column! However, MATLAB makes a vector a row automatically.

Some examples inclue: XYZ coordinates, location, series of daily temperature, samples in an audio signal.
Traits: Norm is the measure of how long a vector is, represented by ∣∣x∣∣||x||. ∣∣[ab..]∣∣=a2+b2+..2||\begin{bmatrix}a&b&..\end{bmatrix}||=\sqrt{a^2 + b^2 + ..^2}
Operations:

  • Multiplication by a scalar "stretches" the vector
  • Addition adds the individual components (only applicable if both vectors are the same size)

Matrix

Matrix size is specified by the number of rows and columns: A = Row x Column.
Two interpretations of a matrix:

  1. As a transform that modifies vectors and vector spaces.

    1. For example, a matrix can rotate and scale vector spaces.
  2. As a container for data (vectors)

    1. For example, images are a rectangular arrangement (matrix) of numbers. Each pixel represents a visual representation of one of these numbers (e.g., 0 is black and 1 is white). Position is important!
  3. As a collection of vectors:

    1. A vertical stacking of row vectors. The space of all vectors that can be composed from the rows of the matrix is called the row space of the matrix.
    2. A horizontal stacking of column vectors.

Example: Audio Signals

  • Audio signals are a sequence of numbers
  • The order in which it occurs is important (ordered in time series), and they represent a perceivable sound.
  • Any sound is a pressure wave, alternating highs and lows of air pressure moving through the air. When we speak, we produce these pressure waves. These waves move our eardrums and we sense it as "sound."
  • Pressure waves move a diaphragm on the microphone. The motion is converted to continuous variations of an electrical signal.
    • A "sampler" samples the continuous signal at regular intervals of time and stores the number.
    • We know the numbers are accurate when we use it to recreate the sense of sound using the numbers to control the levels of an electrical signals, which moves a diaphragm back and forth to produce a pressure wave.

So, a typical digital audio signal is just a sequence of number (a vector)

A sensor should collect data as close to reality as possible, with as little error as possible.

Arrays in MATLAB

Referencing

Elements in the matrix may be referenced by specifying the row and column.

  • You may assign a value to a particular element even if it is outside the current bounds of the array, so if the element is outside, new rows and columns will be added and filled with zeros.

Demo:

Ematrix = [4 7; -2 0] % space or commas work
Ematrix(3,3) = 0 % creates new rows+columns

Shortcut Expressions

Make a long vector by using the colon (:) notation. Works as a for-loop iteration.

Use the ' operator to transpose a matrix.

list = 3:3:10 % [3 6 9]
col = list' % [3; 6; 9]

Compose a matrix out of other matrices using brackets

  • semicolon stacks matrixes by row and comma/space stacks by columns.
mat = [col col col] % [3 3 3; 6 6 6; 9 9 9]

Other shortcuts:

  • zeros creates a matrix of all zeros
  • ones creates a matrix of all ones
  • eye creates a matrix with ones on the diagonal and zeros everywhere else (an identity matrix).
test1 = zeros(3) % [0 0 0; 0 0 0; 0 0 0]
test2 = ones(1,2) % [1 1]
test3 = eye(3,2) % [1 0 0; 0 1 0; 0 0 1]
  • Note If you only put one argument, it will create a square matrix. Otherwise, you can use multiple arguments enclosed in brackets.
  • size function can find the size of the matrix
test1 = zeros(3) % [0 0 0; 0 0 0; 0 0 0]
test2 = ones([1,2]) % [1 1]
test3 = size(test1) % [3 3]
test4 = eye(size(test1)) % [1 0 0; 0 1 0; 0 0 1]

Multidimensional Arrays

Arrays can have more than 2D.

  • Use the length function to return the longest dimension
  • : can be used to denote alll rows or all columns.
a = [1 -3 4: 0 2 -1]
b( :, :, 1) = a
b( :, :, 2) = -a 
size(a) % returns  [2, 3, 2]
length(b) % returns 3

The earlier dimensions (rows) are the very fastest.

In memory, all data is in a big unrolled list, so you can address any array by a single number as if it is a one dimensional vector. So, [1 -3;4 0;2 -1] is [1 0 -3 2 4 -1] in memory. Reshape turns a 2D matrix into a 1D vector.** turns a 2D matrix into a 1D vector.

Colon

: can also be used with the first:incr:last notation to extract portions of array called subaway. end can be substituted for last. This substitutes the dimension in that direction.

a(:, 1:2:end) % returns all rows and every other column till the end

Use it to create an array with values: first, first + incr, first + 2 x incr, first + 3 x incr, ... , last

  • The list ends when the next series would be greater than last element.
  • Ommittting incr causes it to count by 1

: can be used to extract a certain row or column. Without any values before and after, it is the same as saying "all the row/column"

b(:,:,1) = a % means all the first and second dim and the 1st of the 3rd dim

Assignment

When you assign one matrix to another, the dimensions of the two matrices must match.

Operations

These operations act on each element in the array and can be between scalars and vectors/matrices:

  • Addition: must be the same size
  • Subtraction: must be the same size
  • Multiplication: DxN * NxM = DxM. The N should match.
    • Dot Product, or inner product: row vector times column vector = scalar.
    • Outer product or vector direct product: column vectror x row vector = matrix.
    • Multiplying a vector by a matrix transforms it
    • Left multiplication is the dot product of each vector pair. # of row in the second matrix is the # of columns in the first matrix. The result is the # of rows of the first matrix. The vector appears first. It mixes the row vectors of the matrix
    • Right multiplication is the dot product of each vector pair. Multiplication by a matrix transforms a row space vector to a column space vector. It mixes the column vectors of the matrix by using the number in the vector. The vector appears second.
  • Division
  • Exponentiation

Multiplication, division, and exponentiation each have two forms.

  • Array operations .* ./ .^ occur element by element.
  • Matrix operation * / ^ follow linear algebra rules.