Engee documentation
Notebook

Permutations in arrays and changes in their shape

Many Engee functions use array elements and give them a different shape or sequence. This can be useful for data preprocessing, for subsequent calculations, or for data analysis.

Changing the shape

Function reshape modifies the size and shape of the array. For example, transform a matrix of size 3 4 in a matrix of size 2 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 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

Transposing and flipping

A common task in linear algebra is to work with the transpose of a matrix, 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

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

dims = 1 # Working with strings
dims = 2 # Working 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

Displacement and rotation

You can shift the array elements by a certain number of positions using the function circshift. For example, create a matrix of size 3 4 and shift its columns to the right by 2. The second argument [0 2] tells circshift that you need to move rows by 0 places and columns by 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 by 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 the matrix separately in ascending or descending order. The meaning of the dims keyword tells you that work is underway with rows or columns.

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 keyword value rev = true, then 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 reviewed some functions for converting matrices: shape shifting, transposing, shifting and rotating, as well as sorting. You can find more information on operations on matrices in the section [Linear Algebra] (https://engee.com/helpcenter/stable/ru/julia/stdlib/LinearAlgebra.html ).