logo

Functions for 2D Array Operations in Julia 📂Julia

Functions for 2D Array Operations in Julia

Let’s say $A = \begin{pmatrix} 1 & 2 & 1 \\ 0 & 3 & 0 \\ 2 & 3 & 4\end{pmatrix}$.

Transpose Matrix

julia> A =[1 2 1;
          0 3 0;
          2 3 4]
3×3 Array{Int64,2}:
 1  2  1
 0  3  0
 2  3  4

julia> transpose(A)
3×3 LinearAlgebra.Transpose{Int64,Array{Int64,2}}:
 1  0  2
 2  3  3
 1  0  4

julia> A'
3×3 LinearAlgebra.Adjoint{Int64,Array{Int64,2}}:
 1  0  2
 2  3  3
 1  0  4

When the elements of a matrix are real numbers, transpose() and ' return the same matrix, but the data type is subtly different. This is because ' is not exactly transpose but conjugate transpose. Therefore, for real matrices, it effectively returns the same matrix, but for complex matrices, it returns a completely different result.

julia> A_complex=[1+im 2 1+im;
                     0 3 0+im;
                     2 3+im 4]
3×3 Array{Complex{Int64},2}:
 1+1im  2+0im  1+1im
 0+0im  3+0im  0+1im
 2+0im  3+1im  4+0im

julia> transpose(A_complex)
3×3 LinearAlgebra.Transpose{Complex{Int64},Array{Complex{Int64},2}}:
 1+1im  0+0im  2+0im
 2+0im  3+0im  3+1im
 1+1im  0+1im  4+0im

julia> A_complex'
3×3 LinearAlgebra.Adjoint{Complex{Int64},Array{Complex{Int64},2}}:
 1-1im  0+0im  2+0im
 2+0im  3+0im  3-1im
 1-1im  0-1im  4+0im

Power

julia> A =[1 2 1;
          0 3 0;
          2 3 4]
3×3 Array{Int64,2}:
 1  2  1
 0  3  0
 2  3  4

julia> A^2
3×3 Array{Int64,2}:
  3  11   5
  0   9   0
 10  25  18

julia> A*A
3×3 Array{Int64,2}:
  3  11   5
  0   9   0
 10  25  18

julia> A^3
3×3 Array{Int64,2}:
 13   54  23
  0   27   0
 46  149  82

julia> A*A*A
3×3 Array{Int64,2}:
 13   54  23
  0   27   0
 46  149  82

A^2 and A*A return exactly the same result. Likewise, A^3 and A*A*A are the same.

Element-wise Multiplication, Element-wise Division

julia> A =[1 2 1;
          0 3 0;
          2 3 4]
3×3 Array{Int64,2}:
 1  2  1
 0  3  0
 2  3  4

julia> A.*A
3×3 Array{Int64,2}:
 1  4   1
 0  9   0
 4  9  16

julia> A./A
3×3 Array{Float64,2}:
   1.0  1.0    1.0
 NaN    1.0  NaN
   1.0  1.0    1.0

Returns the result of multiplying or dividing each element.

Horizontal Flip, Vertical Flip

julia> A =[1 2 1;
          0 3 0;
          2 3 4]
3×3 Array{Int64,2}:
 1  2  1
 0  3  0
 2  3  4

julia> reverse(A,dims=1)
3×3 Array{Int64,2}:
 2  3  4
 0  3  0
 1  2  1

julia> reverse(A,dims=2)
3×3 Array{Int64,2}:
 1  2  1
 0  3  0
 4  3  2

reverse(A,dims=1) returns the vertically flipped matrix of $A$ and is equivalent to flipud(A) in MATLAB. reverse(A,dims=2) returns the horizontally flipped matrix of $A$ and is equivalent to fliplr(A) in MATLAB.

Inverse Matrix

julia> A =[1 2 1;
          0 3 0;
          2 3 4]
3×3 Array{Int64,2}:
 1  2  1
 0  3  0
 2  3  4

julia> inv(A)
3×3 Array{Float64,2}:
  2.0  -0.833333  -0.5
  0.0   0.333333   0.0
 -1.0   0.166667   0.5

Returns the inverse matrix of $A$. If the inverse matrix cannot be found, it throws an error.

Environment

  • OS: Windows10
  • Version: 1.5.3 (2020-11-09)