Julia 101

MUHAMMAD SALMAN KABIR
2 min readAug 11, 2021

--

Lesson 3.2: Manipulating Matrices Using the Colon Operator

Matrix

In Julia, we can use the colon operator to extract elements from a matrix. This feature, which is similar to MATLAB, is very useful during data analysis. Consider the matrix. returned by the statement:

A = [1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15]
  1. When a colon is used in a matrix reference in place of a specific index number, the colon represents the entire row or column. For example, the statement A[1,:](read as “all columns in row 1”) returns the first row i.e 1 2 3 4 5. Similarly A[:,3] (read as “all rows in column 3”) returns the third column.
  2. The colon operator can also be used to extract multiple rows or columns. For example, the statement A[1:2,:] (read as “all columns from row 1 to row 2”) returns 1 2 3 4 5; 6 7 8 9 10and the statement A[:,1:2] (read as “all rows from column 1 to column 2”) returns 1 2; 6 7; 11 12
  3. To access the last element, last row or last column of a matrix, the keyword “end” can be used. The statements A[end]/A[end,end], A[end,:] and A[:,end] returns 15 , the last row and the last column of matrix A, respectively.
  4. Finally, using a single colon with a matrix name, like A[:] transforms the matrix into a single column. The statement A[1:end] does the same.

--

--

MUHAMMAD SALMAN KABIR
MUHAMMAD SALMAN KABIR

Written by MUHAMMAD SALMAN KABIR

Electrical Engineer | Signal Processing & Machine Learning Enthusiast

No responses yet