Engee documentation
Notebook

Array permutations and shape changes

Many Engee functions use array elements and give them a different shape or sequence. This can be useful for pre-processing data, for later calculations, or for analysing data.

Changing the shape

The reshape function changes the size and shape of an array. For example, convert a matrix of size 3 $\times$ 4 to a matrix of size 2 $\times$ 6.

In [ ]:
A = [1 4 7 10; 2 5 8 11; 3 6 9 12]
Out[0]:
3×4 Matrix{Int64}:
 1  4  7  10
 2  5  8  11
 3  6  9  12
In [ ]:
B = reshape(A,2,6)
Out[0]:
2×6 Matrix{Int64}:
 1  3  5  7   9  11
 2  4  6  8  10  12

Using the elements from A you can create a multidimensional array that will contain 3 matrices of size 2 $\times$ 2.

In [ ]:
C = reshape(A,2,2,3)
Out[0]:
2×2×3 Array{Int64, 3}:
[:, :, 1] =
 1  3
 2  4

[:, :, 2] =
 5  7
 6  8

[:, :, 3] =
  9  11
 10  12

Transpose and flip

A common task in linear algebra is to work with matrix transpose, which turns rows into columns and columns into rows. To do this, use the function transpose or the operator '.

In [ ]:
A = rand(3, 3)
Out[0]:
3×3 Matrix{Float64}:
 0.671146  0.15141   0.445067
 0.205738  0.600742  0.832939
 0.992871  0.645874  0.623041
In [ ]:
A'
Out[0]:
3×3 adjoint(::Matrix{Float64}) with eltype Float64:
 0.671146  0.205738  0.992871
 0.15141   0.600742  0.645874
 0.445067  0.832939  0.623041
In [ ]:
B = [1+im 1-im; -im im]
Out[0]:
2×2 Matrix{Complex{Int64}}:
 1+1im  1-1im
 0-1im  0+1im
In [ ]:
transpose(B) 
Out[0]:
2×2 transpose(::Matrix{Complex{Int64}}) with eltype Complex{Int64}:
 1+1im  0-1im
 1-1im  0+1im

The function reverse moves rows up and down and columns from left to right depending on the value of the keyword dims.

dims = 1 # Work with rows
dims = 2 # Work with columns
In [ ]:
A = [1 2; 3 4]
Out[0]:
2×2 Matrix{Int64}:
 1  2
 3  4
In [ ]:
B = reverse(A, dims=1)
Out[0]:
2×2 Matrix{Int64}:
 3  4
 1  2
In [ ]:
С = reverse(A, dims=2)
Out[0]:
2×2 Matrix{Int64}:
 2  1
 4  3

Offset and rotation

You can shift the elements of an array by a certain number of positions using the function circshift. For example, create a matrix of size 3 $\times$ 4 and shift its columns to the right by 2. The second argument [0 2] tells circshift to shift the rows 0 places and the columns 2 places to the right.

In [ ]:
A = [1 2 3 4; 5 6 7 8; 9 10 11 12]
Out[0]:
3×4 Matrix{Int64}:
 1   2   3   4
 5   6   7   8
 9  10  11  12
In [ ]:
B = circshift(A,[0 2])
Out[0]:
3×4 Matrix{Int64}:
  3   4  1   2
  7   8  5   6
 11  12  9  10

To move the rows from A up 1 and keep the columns in place, specify the second argument as [-1 0].

In [ ]:
C = circshift(A,[-1 0])
Out[0]:
3×4 Matrix{Int64}:
 5   6   7   8
 9  10  11  12
 1   2   3   4
In [ ]:
A = [1 2; 3 4]
Out[0]:
2×2 Matrix{Int64}:
 1  2
 3  4

Sorting

Sorting data in an array is also a valuable tool. For example, the function sort sorts the elements of each row or column of a matrix separately in ascending or descending order. The value of the dims keyword tells you which rows or columns are being sorted.

Create a matrix A and sort each column from A in ascending order.

In [ ]:
A = rand(4,4)
Out[0]:
4×4 Matrix{Float64}:
 0.461938  0.0393388  0.227309  0.0815622
 0.278592  0.850108   0.563387  0.646155
 0.385563  0.42562    0.851425  0.859178
 0.655341  0.601584   0.659288  0.238689
In [ ]:
B = sort(A, dims=1) 
Out[0]:
4×4 Matrix{Float64}:
 0.278592  0.0393388  0.227309  0.0815622
 0.385563  0.42562    0.563387  0.238689
 0.461938  0.601584   0.659288  0.646155
 0.655341  0.850108   0.851425  0.859178

If you specify the value of the keyword rev = true, the sorting will not be in ascending order, but in descending order.

In [ ]:
C = sort(A, dims=2, rev=true)
Out[0]:
4×4 Matrix{Float64}:
 0.461938  0.227309  0.0815622  0.0393388
 0.850108  0.646155  0.563387   0.278592
 0.859178  0.851425  0.42562    0.385563
 0.659288  0.655341  0.601584   0.238689

Conclusion

In this article we have considered some functions for matrix transformation: reshaping, traspanning, shifting and rotating, and sorting. More information on matrix operations can be found in Linear Algebra.