GNU Octave Part 3 — Matrices

Aung Baw
3 min readMar 23, 2021
Photo by Markus Spiske on Unsplash

In this part we gonna explore basic arithmetic for matrices and explore octave lib for better productivity.

Arithmetic operation

a = [1 2; 3 4];
b = [4 5; 6 7];
a + b
5 7
9 11
a - b
-3 -3
-3 -3
a * b % matrix multiplication
16 19
36 43
a .* b % element-by-element multiplication
4 10
18 28
a / b % (inverse (b') * a')'
2.5000 -1.5000
1.5000 -0.5000
-a % negation
-1 -2
-3 -4
a' % transpose, change row into column
1 3
2 4
a ^ 2 or mpower(a,2)
7 10
15 22
(1:0.5:5) % : column is slicing operator
1.0000 1.5000 2.0000 2.5000 3.0000
3.5000 4.0000 4.5000 5.0000
a(1,:) % accessing entire row 1

Note: both matrix addition and element-by-element addition are equivalent, same for subtraction.

Basic Matrices Helper

a = [1 2; 3 4; 5 6];rows(a)      % will get num of rows in 'a' matrix 
3
columns(a) % will get num of columns in 'a' matrix
2
length(a) % will get largest row or column count in 'a' matrix
3
numel(a) % will get every index of a matrix
6
iscell(a) % check whether it's cell or matrix or else
0

Cell()

Creating a cell array of a given size, containing empty matrices. It’s like a Wrapper in JavaScript or Python. You can use it to initiate the starting rows/columns empty cells, or you can insert data right away and it will dynamically create cell for you.

just_a_cell = cell(2,2)   % simple cell arrays
{
[1,1] = [](0x0)
[2,1] = [](0x0)
[1,2] = [](0x0)
[2,2] = [](0x0)
}
cell_with_data = {"a string", rand(2, 2)};cell_with_data(1)
{
[1,1] = a string
}
cell_with_data(2)
{
[1,1] =
0.8221 0.7137
0.9996 0.9821
}% Existing matrix could be

Num2Cell()
Alternative of cell() method, creating empty cell arrays, and then filling them up. It also work on existing matrix. There is similar method for string called cell2str() work exactly like this fun.

old_data = [1 2; 3 4];
num2cell (old_data)
{
[1,1] = 1
[2,1] = 3
[1,2] = 2
[2,2] = 4
}
s = ["hello"; "world"];
newcell = cellstr(s)
{
[1,1] = hello
[2,1] = world
}

Mat2cell()

Convert the matrix A to a cell array. There is vice versa method called cell2mat().

x = reshape (1:12, [3, 4])';    % 1 to 12, make it 3 rows 4 columns
mat2cell (x, [1,2]) % divided in to two cell elements
{
[1,1] =
1 4 7 10
2 5 8 11
[2,1] = 3 6 9 12}

Struct

Structure are like key-value paired object, you can loop through them & can access value by obj.key

cell to struct will convert your indexed cells to key-value structures.

c2s = cell2struct({"Peter", "Hannah", "Robert"; 185, 170, 168}, {"Name","Height"});c2s(1)
{
Name = Peter
Height = 185
}

struct to cell

s = struct ("name", {"Peter", "Hannah", "Robert"},
"age", {23, 16, 3});
c = struct2cell(s)
{2x1x3 Cell Array}

Zeros & Ones

Creating matrix or N-dimensional array whose elements are all 0 or 1.

zeros(2);      % value 0, 2x2 matrix
zeros(2,3); % value 0, 2 rows 3 columns matrix
zeros(2); % value 1, 2x2 matrix
zeros(2,3); % value 1, 2 rows 3 columns matrix

Rand and Randn

Rand return a matrix with random elements uniformly distributed on the
interval (0, 1). Randn return a matrix with normally distributed random elements having zero mean and variance one.

rand(4)
0.129093 0.387299 0.198145 0.794909
0.472483 0.758729 0.518242 0.598641
0.244235 0.264744 0.505411 0.823889
0.222329 0.064007 0.233149 0.903406

Identity

Will return an identity matrix. Let I be identity matrix and you get following

I * A = A * I = A

eye(3)1   0   0
0 1 0
0 0 1

Inverse Matrix

there is no concept of dividing by a matrix

We need inverse of a matrix to achieves the same thing.

A = [1 2; 3 4];inv(A)              % inv is alias of inverse
-2.0000 1.0000
1.5000 -0.5000
A * inv(A)
1.0000 0
0.0000 1.0000

NOTE: a matrix must be a square matrix in order to use this function

--

--

Aung Baw

Focusing on security, cloud, and DevOps, I am a lifelong learner and lazy 徒弟.