Basic Vector and Matrix Operations in Julia: Quick Reference and Examples

Basic Vector and Matrix Operations in Julia: Quick Reference and Examples

Last updated:
Basic Vector and Matrix Operations in Julia: Quick Reference and Examples
Source

All examples were executed under Julia Version 0.3.10

Matrices

Matrices are probably one of the data structures you'll find yourself using very often. For Julia, Vectors are just a special kind of Matrix, namely with just one row (row matrix) or just one column (column matrix):

Julia Vectors can come in two forms: Column Matrices (one column, N rows) and Row Matrices (one row, N columns)

Row Matrix

Spaces between elements:

julia> [1 2 3]
1x3 Array{Int64,2}:
 1  2  3

Column Matrix

' means transpose:

julia> [1 2 3]'
3x1 Array{Int64,2}:
 1
 2
 3

Generic Matrix

Spaces between elements and semicolons between rows:

julia> [1 2 3; 4 5 6 ]
2x3 Array{Int64,2}:
 1  2  3
 4  5  6

Useful Operations

  • size

Returns the size of one dimension of your collection:

 julia> size([1 2 3],1)
 1
 julia> size([1 2 3],2) 
 3
  • reshape(n,m)

Very useful operation. Takes any collection and returns a representation containing n rows and m columns:

 julia> matrix = [1 2 3; 4 5 6] 
 2x3 Array{Int64,2}:
  1  2  3
  4  5  6

 julia> reshape(matrix,1,6)
 1x6 Array{Int64,2}:
  1  4  2  5  3  6

 julia> reshape(matrix,6,1)
 6x1 Array{Int64,2}:
  1
  4
  2
  5
  3
  6

An error will be raised if you try to use invalid dimensions:

 julia> reshape(matrix,7,1)
 ERROR: DimensionMismatch("new dimensions (7,1) must be consistent with array size 6")
  in reshape at array.jl:89
  in reshape at abstractarray.jl:127
  • hcat

Horizontal concat. Stacks two or more matrices, horizontally:

 julia> matrix = [1 2 3; 4 5 6]
 2x3 Array{Int64,2}:
 1  2  3
 4  5  6

 julia> hcat(matrix,[9;9])
 2x4 Array{Int64,2}:
  1  2  3  9
  4  5  6  9

You'll get an error if the matrices don't have the same number of rows!

 julia> hcat(matrix,[9 9])
 ERROR: number of rows must match
  in hcat at abstractarray.jl:571
  • vcat

Vertical concat. Stacks two or more matrices, on top of each other:

  julia> matrix = [1 2 3; 4 5 6]
 2x3 Array{Int64,2}:
  1  2  3
  4  5  6

 julia> vcat(matrix,[9 9 9])
 3x3 Array{Int64,2}:
  1  2  3
  4  5  6
  9  9  9

Error if the two matrices don't have the same number of columns!

 julia> vcat(matrix,[9 9])
 ERROR: number of columns must match
  in vcat at abstractarray.jl:598
  • map

Pass every element of the collection through a function and return the new collection.

In this example, divide every element of a vector by the sum of all elements, in other words, normalize it:

 julia> vector = [1 2 3 4 5 6]
 1x6 Array{Int64,2}:
  1  2  3  4  5  6

 julia> map(x-> x/sum(vector),vector)
 1x6 Array{Float64,2}:
  0.047619  0.0952381  0.142857  0.190476  0.238095  0.285714
  • shuffle

Shuffles an array's elements:

This is for 1-D Arrays

 julia> shuffle([1,2,3])
  3-element Array{Int64,1}:
 2
 1
 3

Observations

Technically, there's still another way to create Vectors in Julia but they are not as widely used and will probably cause some confusion.

Remember that you can create an Array of Strings (or any other type, really) using commas between the elements:

julia> ["foo","bar","baz"]
3-element Array{ASCIIString,1}:
 "foo"
 "bar"
 "baz"

So you can also create an Array of Integers:

julia> [1,2,3]
3-element Array{Int64,1}:
 1
 2
 3

These are commonly referred to as 1-D Arrays.

Note that these can't be used in operations involving matrices (because, unlike column matrices and row matrices, they aren't matrices ) so it's better not to use these when you're doing linear algebra or things like that, unless you use stuff like reshape on it.


More info