Compose2020 Manipulate Matrices in OML
 Create a matrix.
 Slice a matrix.
 Extract a matrix.
 Perform simple operations on matrices.
Creating a Matrix
Matrices are the backbone of OML and can hold floats, characters, and complex numbers.

To create a matrix, use brackets [], as shown in the example below:
a = [1,2;3,4]
You can also create the same matrix using the following commands:
a = [1 2;3 4] a = [1 2 3 4]

Click Run. The three assignments return the
following:

For a complex matrix, use
i
in the matrix element or use a complex function:a = [1 2 ; 3+i 4+i]
or
a = complex([1 2;3 4],[0 0;1 1])
Both will return this result:
Slicing a Matrix

If you want to slice a whole row or column from that matrix, use
:
in the first column:a = [1,2;3,4] a(:,1)
This extracts the first column of the matrixa
: 
To slice several rows or columns from that matrix, use an array to specify the
rows or columns:
a = [1 2 3;4 5 6;7 8 9]; % the first and third rows of a a([1, 3],:)
This extracts the first and third rows of the matrix
a
: 
When slicing a matrix,
end
stands for the last index of a row or column.a = [1 2 3;4 5 6;7 8 9]; a(end,:)
This returns the last row of matrix
a
:a = [1 2 3;4 5 6;7 8 9]; a(:,end1)
This script returns thesecond last column of a
:
Extracting a Matrix

If you want to extract a specific element in a matrix, you can use
a(x,y)
witha
being the name of the matrix andx
andy
being the row and column being referenced, respectively. For example:a = [1,2;3,4] b = a(1,1)
Results in:

If you want to extract specific elements in a matrix, use two arrays to specify
the rows and columns being referenced, respectively. For example:
a = [1 2 3;4 5 6;7 8 9]; a([1 3],[2 1])
[1 3] stands for the first and third rows, [2 1] stands for the second and first column:

A shortcut to extract the last elements in an array is to use
end. See the example below:
a = [1 2 3;4 5 6;7 8 9] b = a([2,3],[2:end])
This results in:

Assignment operations can be joined. Below is an example to extract an element
from a sliced matrix:
a = [1 2 3;4 5 6;7 8 9] % return to the third row of a a(3,:) % return to the second element of the third row a(3,:)(2)
Performing Operations on Matrices

Most simple matrix operations can be done in the same way you perform
operations on integers. To multiply two matrices of the same size, write the
following:
a = [1,2;3,4] b = [2,3;4,5] a*b
The result is:
You could use
a.*b
to perform entrywise multiplication:Please refer to the Arithmetic Operation help document for more information on the other operations.

To use the matrix in a builtin function, such as transpose,
simply write:
transpose([1,5,2;6,2,3])
or, you could use
.'
for transpose, both return the same result:For more matrix operations, please refer to the script simple_matrix.oml, located in the Tutorials folder in the installation directory.