e-Mathematics > Matrix Algebra

Arrays

All variables are arrays in matlab/octave, consisting of one or more elements in a form of scalar, vector, or matrix.
> x =2
> x(3) = 5
> x(2,2) = -1
Initially x is created as a scalar of the value 2, and expanded to a row vector then to a matrix in order to assign the value to the third entry x(3) and then the value to the (2,2) entry x(2,2).

It is possible to select a submatrix (block) from a matrix by using an array subscript.

> A = [1 -2 1; 0 2 -8; -4 5 9]
> A(1,2)
> A([1 2],[1 3])
> A(:,3)
> A(2,:)
A colon (:) can be used in a subscription to select all of the values of either row (at the first subscript) or column (at the second subscript).

Square brackets ([]) are used for array concatenation for two matrices with the same size of rows.

> A = [1 -2 1; 0 2 -8; -4 5 9]
> B = [3 1; -2 1; 0 6]
> C = [A B]

The function zeros(m,n) or ones(m,n) are used to create an $ m\times n$ matrix with all 0's or all $ 1$'s. They are useful for initializing a matrix of a fixed size. The size function size(A) returns the number of rows and columns.

> A = zeros(5,7)
> size(A)

The colon (:) is used for generating a row vector. For example, 3:5 generates [3 4 5]. If you have a particular choice for the stepping increment, you can use

> v = 1:2:10
This returns v = [1 3 5 7 9]. To reverse the entries in the vector v we can use
> w = v(end:-1:1)
which gives w = [9 7 5 3 1].


© TTU Mathematics