Manipulating matrices in Julia

Matrices in Julia are the heterogeneous type of containers and hence, they can hold elements of any data type. It is not mandatory to define the data type of a matrix before assigning the elements to the matrix. Julia automatically decides the data type of the matrix by analyzing the values assigned to it. Because of the ordered nature of a matrix, it makes it easier to perform operations on its values based on their index.

Following are some common matrix manipulation operations in Julia:

  • Transpose of a matrix
  • Flipping a matrix
  • Concatenating matrices
  • Reshaping a matrix
  • Inverse of a matrix

Creating a matrix

Julia provides a very simple notation to create matrices. A matrix can be created using the following notation: A = [1 2 3; 4 5 6]. Spaces separate entries in a row and semicolons separate rows. We can also get the size of a matrix using size(A).

Transpose of a matrix



  • The transpose operation flips the matrix over its diagonal by switching the rows and columns.
  • Let A be a matrix. We can get the transpose of A by using A’.
  • Example 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Defining a square matrix of size (2, 2)
    A = [1 2; 3 4]    
      
    # Transpose of A 
    A'               

    chevron_right

    
    

    Output:

    Example 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Defining a retangular matrix of size (2, 3)
    B = [1 2 3; 4 5 6]   
      
    # Transpose of B
    B'                   

    chevron_right

    
    

    Output:

Flipping a matrix:

  • A matrix in Julia can be flipped via the X-axis i.e. horizontally or via the Y-axis i.e. vertically.
  • To flip the matrix we use reverse(< matrix >, dims= < 1 or 2 >)) 1 = vertically, 2 = horizontally.

Example 1: Flipping vertically

filter_none

edit
close

play_arrow

link
brightness_4
code

# Defining a rectangular matrix of size (2, 3)
B = [1 2 3; 4 5 6]    
  
# Flipping the matrix vertically
reverse(B, dims = 1)   

chevron_right


Output:

Example 2: Flipping horizontally

filter_none

edit
close

play_arrow

link
brightness_4
code

# Flipping the matrix horizontally
reverse(B, dims = 2)   

chevron_right




Concatenating matrices

  • In Julia we can concatenate a matrix to another matrix to the right side of the initial matrix or to the bottom of it.
  • We use vcat(A, B) to concatenate to the side.
  • And hcat(A, B) to concatenate to the bottom.
  • While concatenating to the side, we need to make sure that both the matrices have same number of rows.
  • While concatenating to the bottom, we need to make sure that both the matrices have same number of columns.

Example 1: Concatenate to the side

filter_none

edit
close

play_arrow

link
brightness_4
code

# Creating a square matrix of size (2, 2)
A = [1 2; 3 4]        
  
# Creating a rectangular matrix of size (2, 3)
B = [5 6 7; 8 9 10]   
hcat(A, B)

chevron_right


Example 2: Concatenate to the bottom

filter_none

edit
close

play_arrow

link
brightness_4
code

# Creating a square matrix of size (3, 2)
A = [1 2;3 4; 5 6]        
  
# Creating a rectangular matrix of size (4, 2)
B = [5 7;8 9; 10 11;14 16]   
vcat(A, B)

chevron_right


Reshaping a matrix


We can reshape a matrix into another matrix of different size.
Example 1: Reshaping a matrix

filter_none

edit
close

play_arrow

link
brightness_4
code

# The original matrix with size (3, 2)
A = [1 2; 3 4; 5 6]    

chevron_right


  • Output:
  • Reshaping the matrix to size (2, 3)

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    reshape(A, (2, 3))

    chevron_right

    
    

    Output:

    Reshaping the matrix to size (6, 1)



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    reshape(A, (6, 1))

    chevron_right

    
    

    Reshaping the matrix to size (1, 6)

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    reshape(A, (1, 6))

    chevron_right

    
    

    Output:

    Inverse of a matrix