Operations

You will find in this section information on how to apply basic matrix operations.

Transpose

Let's create the following matrix m:

>> m = [5 8 0; 2 -1 -2; 3 6 7]# 

% matrix m initialization

5 8 0 2 -1 -2 3 6 7

The single quote operator allows to transpose a matrix:

>> m'# 

% matrix m transpose

5 2 3 8 -1 6 0 -2 7

Concatenation

Let's define two matrices m1 and m2 as follows:

>> m1 = [1 3; 2 4]# 

% matrix m1 initialization

1 3 2 4 >> m2 = [5 7; 6 8]#

% matrix m2 initialization

5 7 6 8

In order to concatenate vertically m1 and m2 you can use brackets:

>> [m1; m2]# 

% m1 and m2 vertical concatenation

1 3 2 4 5 7 6 8

and similarly to concatenate horizontally m1 and m2:

>> [m1 m2]# 

% m1 and m2 horizontal concatenation

1 3 5 7 2 4 6 8

Removing Rows and Columns

Removing rows or columns from a matrix is done by assigning empty brackets to the rows or columns you want to remove.

The following code removes the second column from matrix m:

>> m(:,2) = []# 

% deleting second column from matrix m

5 0 2 -2 3 7

and similarly to remove the first row from matrix m:

>> m(1,:) = []# 

% deleting first row from matrix m

2 -1 -2 3 6 7

Adding and Substracting

Adding and substracting matrices is straightforward. Matrices will need to have consistent dimensions for the operation to be accepted.

>> m1 + m2# 

% adding matrices m1 and m2

6 10 8 12 >> m1 - m2#

% substracting matrix m2 from m1

-4 -4 -4 -4

Multiplication, Power and Division

Matrix multiplication (*), power (^) and division (/) must be distinguished from their element-wise siblings (cf. next section). Matrix multiplication is to be understood in the mathematical sense where multiplying an n x m matrix by against a p x q matrix results in an n x q matrix. For the latter the constraint is to have m equals p.

Let's define the vector v as follows:

>> v = [1; 2; 3]# 

% vector v initialization

1 2 3

and calculate m multiplied by v:

>> m * v# 

% 3x3 matrix m mutliplied by 3x1 vector x

21 -6 36

Matrix power and division are also to be understood in the mathematical sense. mathlayer® handles power for square matrices only:

>> m^3# 

% m to the power of 3

221 200 -176 -16 -61 -94 471 546 139

Right division between matrices is equivalent to the multiplication of the first matrix against by the inverse of the second one:

>> m1 / m2# 

% dividing m1 by m2

5 -4 4 -3 >> m1 * inv(m2)#

% recovering same result using matrix inversion

5 -4 4 -3

Left division solves linear equation systems, for instance to solve for x m * x = v:

>> x = m \ v# 

% let's solve for x

1.14815 -0.59259 0.44444 >> m * x#

% checking vector v is recovered

1 2 3

Element-wise operations

Element-wise operations allow - as the name suggests - to apply operations on matrices element by element. Element-wise multiplication (.*), left division (.\), right division (./) and power (.^) are available. Matrices must have consistent dimensions, except if:
  • One of the matrices is a scalar
  • One of the matrices is a vector with a number of elements consistent with the number of rows or columns of the other matrix
>> m1 .* m2#	

% element-wise multiplication

5 21 12 32 >> m1 ./ m2#

% element-wise right division

0.2 0.428571 0.333333 0.5 >> m1 .\ m2#

% element-wise left division

5 2.33333 3 2 >> m1 .^ m2#

% element-wise power

1 2187 64 65536 >> m * v#

% matrix against vector element-wise multiplication

5 8 0 4 -2 -4 9 18 21 >> 3 .* m#

% scalar against matrix

15 24 0 6 -3 -6 9 18 21