Vectors and matrices are very useful for dealing with systems of linear equations and inequalities. In particular, they are required for compactly representing and discussing the linear programming problem. Octave being a scientific programming language have a rich support for matrices and vectors, same as Matlab. A vector is a list of numbers which can be in a row or column, and matrix is an array of numbers with one or more rows, & one or more columns. Matrix is not only limited to rows and columns only, there is possibility of n-dimensional array. A digital “RGB” image is a good example of three dimensional matrix, with rows and columns it will have 3 planes, one for each color. It should be very clear by definition that vector is just a subset of matrix and hence both follows similar programming practice. This article takes more practical approach than theoretical. It is expected that reader have basic knowledge of high school mathematics, to know about matrix and its operations. Like the earlier articles, we encourage readers planning to learn octave/Matlab to try the exercises marked as “Ex“ along with the examples used in this article.
Initialization
Row Vector
A row vector can be initialized with numbers inside braces separated by comma “,” or spaces ” “.
>>rowVec1 = [1,2,3,4]
rowVec1 =
1 2 3 4
>>rowVec2 = [1 2 3 4]
rowVec2 =
1 2 3 4
linspace function can also be used to create a row vector with equally distanced elements.
vectorName = linspace(startValue, endValue, numberOfElements)
>>rowVec3 = linspace(1, 10, 5)
rowVec3 =
1.0000 3.2500 5.5000 7.7500 10.0000
Ex: Try to find out, what happens when linspace function is used without specifying numberOfElements.
Column Vector
Column vector are initialized with numbers inside braces separated by semicolon “;” or newline(enter key).
>>colVec1 = [1;2;3;4]
colVec1 =
1
2
3
4
>>colVec2 = [1
2
3
4]
colVec2 =
1
2
3
4
Matrix
Matrix can be initialized using row vector and column vector initialization method together.
>>mat1 =[1,2,3;4,5,6;7,8,9]
mat1 =
1 2 3
4 5 6
7 8 9
>>mat2 = [1 2 3 4
5 6 7 8]
mat2 =
1 2 3 4
5 6 7 8
Another method of creating a matrix is by use of a colon “:” operator in along with assignment. if a Variable is defined with row and column number parameter, program automatically creates a matrix with size defined by parameter and assigns the value at place.
>> mat3(1:2,1:2) =8
mat3 =
8 8
8 8
>>mat4(2,2) = 7
mat4 =
0 0
0 7
One important thing to note here is that indexing starts from ‘1’ in Matlab/Octave unlike other programming language.
Ex: Create a 3 dimensional matrix with rows = 3, cols = 3 and planes = 3, and all elements having a value '0'.
Special Utility Matrices
For common utility related to matrices, there are several functions developed which are collectively known as special utility matrices.
zeros/ones
Matrices can be initialized with all zeros or ones using the function with same name.
matName = zeros(numRows, numColumns)
>>mat5 = zeros(2,4)
mat5 =
0 0 0 0
0 0 0 0
>> mat6 = ones(4, 2)
mat6 =
1 1
1 1
1 1
1 1
Using the same method one can also generate the vector by selecting row number == 1 for row vector, or column number == 1 for column vector.
>>rowVec4 = zeros(1, 4)
row_vec3 =
0 0 0 0
>>colVec3 = ones(3,1)
col_vec3 =
1
1
1
For creating square matrices it is not needed to give both row count and column count.
>>mat7 = zeros(5)
mat7 =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
eye
Identity matrix can be formed with eye function.
>>mat8 = eye(3)
mat8 =
Diagonal Matrix
1 0 0
0 1 0
0 0 1
repmat
To create a matrix with same number as each element repmat is the function to use.
matName = repmat(number, sizeOfMatrix)
>>mat8 = repmat(5,2)
mat8 =
5 5
5 5
to make non square matrix, dimension may also be supplied as vector.
>>mat9 = repmat(5,[2,4])
mat9 =
5 5 5 5
5 5 5 5
rand
rand function used for generating a random number can also be used for generating random matrix.
>>rand()
ans = 0.99085
>> mat10 = rand(3)
mat10 =
0.167886 0.890964 0.399979
0.893061 0.014076 0.589449
0.749548 0.517955 0.743926
>> mat11 = rand([2,3])
mat11 =
0.584307 0.061261 0.875626
0.840153 0.528905 0.793453
Ex: Functions used above might be used with more arguments. Discover them using help command. Ex: Here all special utility matrices are not discussed. Find more in octave documentation.
Basic Operations
Addition and Subtraction
Simple addition does the element wise addition. Same goes for subtraction.
>> mat1+mat1
ans =
2 4 6
8 10 12
14 16 18
>> mat1-mat1
ans =
0 0 0
0 0 0
0 0 0
Scalar Multiplication and Division
Multiplying or dividing any matrix by a number is same as doing the same operation with each element separately.
>> mat1*3
ans =
3 6 9
12 15 18
21 24 27
>> mat1/2
ans =
0.50000 1.00000 1.50000
2.00000 2.50000 3.00000
3.50000 4.00000 4.50000
Element wise Operations
Using the dot operator matrix or vector can perform element wise operation.
mat = [1,2,3;3,2,1]
mat =
1 2 3
3 2 1
>> mat.*mat
ans =
1 4 9
9 4 1
>> mat.^0.5
ans =
1.0000 1.4142 1.7321
1.7321 1.4142 1.0000
>> mat./mat
ans =
1 1 1
1 1 1
Transpose Operation
Using the transpose operator rows and columns can be interchanged. It can be handy in converting row vector to column vector and vice versa.
>> rowVec = [1,2,3]
rowVec =
1 2 3
>> colVec = rowVec'
colVec =
1
2
3
Multiplication and Division
To multiply an m×n matrix by an n×p matrix, the ‘n’s must be the same, and the result is an m×p matrix.
>> mat = [1,2,3;4,5,6]
mat =
1 2 3
4 5 6
>> mat2 = [1,2;3,4;5,6]
mat2 =
1 2
3 4
5 6
>> mat3 = mat*mat2
mat3 =
22 28
49 64
>> mat3/mat2
ans =
1.00000 2.00000 3.00000
4.00000 5.00000 6.00000
Ex: What is the condition for matrix division so that no error is produced.
Left Division
X = A\B solves the symbolic system of linear equations in matrix form, A*X = B for X. If the solution does not exist or if it is not unique, the \ operator issues a warning.
Ex: Try to solve some linear equations with left division.
Rank Norm and Determinant
mat = [1,2,3;3,2,1]
mat =
1 2 3
3 2 1
>> rank(mat)
ans = 2
>> norm(mat)
ans = 4.8990
>> det(mat)
error: det: A must be a square matrix
>> det(mat(:,1:2))
ans = -4
Slicing
In the determinant example, a matrix slicing is used.
“:” replacing row number with colon operator indicates, all rows.
“1:2” replacing column number with column operator indicates to get columns only from 1 to 2.
In general column operator can be used with 3 different syntax.
- ” : ” indicates all elements.
- “startNum : endNum” indicates all elements between start and end both inclusive.
- “startNum :n: endNum” indicates every n’th element from start till end.
Selfhelp
- “diag” command can fill all elements of a row/column vector across the diagonal of a diagonal matrix.
- try logspace(a,b,n) function, which return a row vector with n elements logarithmically spaced from 10^a to 10^b.
- learn different uses of randi()function for generating random integers, with help command.
- check documentation for “famous matrices“. Generate different famous matrices with single function.
This article serves as starting point for utilizing the power of vectors and matrices in Octave/Matlab. There is a lot to learn as these languages heavily emphasize on matrix and related operations. Let us know your query and questions via comment section.
- GNU Octave – Scientific Programming Language
- Octave:1> Getting Started
- Octave:1> Variables and Constants
- Octave:1> Expressions and operators
- Octave:1> Built-In Functions, Commands, and Package
- Octave:2> Vectors and Matrices Basics
- Octave:2> Scripts and Algorithm
- Octave:2> Conditional Execution
- Octave:2> Functions
- Octave:3> File Operations
- Octave:3> Data Visualization