Create a Matrix


In [28]:
A = [1 2 3; 5 6 7; 12 19 22]


Out[28]:
3x3 Array{Int64,2}:
  1   2   3
  5   6   7
 12  19  22

' is the transpose operator

You can use the transpose() method, but using ' is often more convenient


In [29]:
A'


Out[29]:
3x3 Array{Int64,2}:
 1  5  12
 2  6  19
 3  7  22

⁻¹ is not the inverse operator

There is no inverse operator. Instead you can use the inv() method, or just raise the matrix to the -1th power


In [30]:
A^-1


Out[30]:
3x3 Array{Float64,2}:
 -0.0625   0.8125  -0.25
 -1.625   -0.875    0.5 
  1.4375   0.3125  -0.25

⋅ is the dot product operator

Type \cdot<Tab> to get this operator. You can also call it as a function using dot(A, B)

In Julia 0.4, for multi-dimensional matrices, we need to use the vecdot function instead.


In [31]:
vecdot(A', (A^-1))


Out[31]:
3.000000000000001

Similarly, × is the cross product operator

Type \times<Tab> to get this operator, or use the cross(A, B) function.

The regular rules for matrix multiplication hold.

Vectors

A vector is a special Matrix that has only 1 column. Remember that Julia's arrays are column major, even if they are displayed as a row here (that's just to make a compact display)


In [32]:
a = [1 2 3 4 5]
b = [3 5 7 9 11]


Out[32]:
1x5 Array{Int64,2}:
 3  5  7  9  11

∩ is the Set intersection operator

Type \cap<Tab> to get this operator


In [33]:
a  b


Out[33]:
2-element Array{Int64,1}:
 3
 5

∪ is the Set union operator

Type \cup<Tab> to get this operator


In [34]:
a  b


Out[34]:
8-element Array{Int64,1}:
  1
  2
  3
  4
  5
  7
  9
 11

And there are many more set operators

\in, \ni, \subseteq


In [35]:
3  a


Out[35]:
true

In [36]:
4  b


Out[36]:
false

In [37]:
b  3


Out[37]:
true

In [38]:
a  b


Out[38]:
false

hcat concatenates two arrays horizontally


In [39]:
hcat(a, b)


Out[39]:
1x10 Array{Int64,2}:
 1  2  3  4  5  3  5  7  9  11

vcat concatenates two arrays vertically


In [40]:
vcat(a, b)


Out[40]:
2x5 Array{Int64,2}:
 1  2  3  4   5
 3  5  7  9  11

You can also add and subtract two vectors


In [41]:
a + b


Out[41]:
1x5 Array{Int64,2}:
 4  7  10  13  16

Remember that dimensions must match


In [42]:
A + a


LoadError: DimensionMismatch("dimensions must match")
while loading In[42], in expression starting on line 1

 in promote_shape at operators.jl:220
 in + at arraymath.jl:96

But if one of the operands is a scalar it's okay


In [43]:
A * 2


Out[43]:
3x3 Array{Int64,2}:
  2   4   6
 10  12  14
 24  38  44

In [44]:
A + 2


Out[44]:
3x3 Array{Int64,2}:
  3   4   5
  7   8   9
 14  21  24

Some functions only accept vectors, not arrays

Use the vec() function to turn a generic single dimensional array into a column vector


In [45]:
cor(vec(a), vec(b))


Out[45]:
0.9999999999999998

In [ ]: