Matrix and Vector

The data structure array is used in Julia for matrix algebra. Let's start with constructing matrices and vectors.

<font,color="blue">construct a matrix</font>

In [10]:
X1=[1 2 1
   2 1 3]

X2=[1 2 1; 2 1 3]
Out[10]:
2x3 Array{Int64,2}:
 1  2  1
 2  1  3
In [11]:
println("The type of X1 is ",typeof(X1))
println("The type of X2 is ",typeof(X2))
The type of X1 is Array{Int64,2}
The type of X2 is Array{Int64,2}

The typeof( ) function is used to check the type of the matrix.

Array{Int64,2}

This array has 2 dimension and the type of the elements contained in the array is Int64.

<font,color="blue">construct a vector</font>

In [12]:
y=[1,2,3]
Out[12]:
3-element Array{Int64,1}:
 1
 2
 3
In [13]:
println("The type of y is ",typeof(y))
The type of y is Array{Int64,1}

A vector is a one-dimension array.

<font,color="blue">construct a special matrix or vector</font>

In [2]:
println("construct an empty vector")
println(Array(Float64,0))

println("construct another empty vector")
println(Float64[])

println("construct a linearly-spaced vector")
println([1:2:10])

println("construct a matrix of 1s")
println(ones(2,3))

println("construct a matrix of 0s")
println(zeros(2,3))

println("construct a matrix of true")
println(trues(2,3))

println("construct a matrix with elements sampled from random normal then modify it")
A=round(randn(2,3),2)
println(A)
A[1,2]=1000
println(A)

println("construct a heterogeneous vector")
a=cell(2); a[1]=10; a[2]="hello"
println(a)
construct an empty vector
[]
construct another empty vector
[]
construct a linearly-spaced vector
[1,3,5,7,9]
construct a matrix of 1s
[1.0 1.0 1.0
 1.0 1.0 1.0]
construct a matrix of 0s
[0.0 0.0 0.0
 0.0 0.0 0.0]
construct a matrix of true
Bool[true true true
     true true true]
construct a matrix with elements sampled from random normal then modify it
[-0.18 -0.93 -0.86
 -0.63 0.89 0.39]
[-0.18 1000.0 -0.86
 -0.63 0.89 0.39]
construct a heterogeneous vector
{10,"hello"}

<font,color="blue">Basic funtions for arrays</font>

In [15]:
X=[1 2 1 2;1 3 1 2]
Out[15]:
2x4 Array{Int64,2}:
 1  2  1  2
 1  3  1  2
In [16]:
println("The number of elements in X is ",length(X))
println("The number of rows and columns in X is ",size(X))
println("The number of columns in X is ",size(X,2))
The number of elements in X is 8
The number of rows and columns in X is (2,4)
The number of columns in X is 4

<font,color="blue">Learn other functions for array by an example</font>

<font,color="charcoal">1. declare an uninitilazed matrix $M$</font>

In [17]:
M=Array(Float64,2,3) #or Float64[2,3]
Out[17]:
2x3 Array{Float64,2}:
 2.22719e-314  2.22728e-314  2.22727e-314
 2.22722e-314  2.22729e-314  2.22714e-314

<font,color="charcoal">2. fill $M$ with a value</font>

In [18]:
fill!(M,3.0)
M
Out[18]:
2x3 Array{Float64,2}:
 3.0  3.0  3.0
 3.0  3.0  3.0

<font,color="charcoal">3. construct a $3\times 3$ identity matrix</font>

In [19]:
I=eye(3)
Out[19]:
3x3 Array{Float64,2}:
 1.0  0.0  0.0
 0.0  1.0  0.0
 0.0  0.0  1.0

<font,color="charcoal">4. concatenate $M$ and $I$ vertically</font>

In [20]:
M2=vcat(M,I)
Out[20]:
5x3 Array{Float64,2}:
 3.0  3.0  3.0
 3.0  3.0  3.0
 1.0  0.0  0.0
 0.0  1.0  0.0
 0.0  0.0  1.0

<font,color="charcoal">5. reshape $M2$</font>

In [21]:
M2=reshape(M2,3,5)
Out[21]:
3x5 Array{Float64,2}:
 3.0  0.0  3.0  0.0  0.0
 3.0  0.0  0.0  3.0  0.0
 1.0  3.0  1.0  3.0  1.0

<font,color="charcoal">6. copy $M2$ to $M3$</font>

In [22]:
M3=copy(M2)
Out[22]:
3x5 Array{Float64,2}:
 3.0  0.0  3.0  0.0  0.0
 3.0  0.0  0.0  3.0  0.0
 1.0  3.0  1.0  3.0  1.0

<font,color="charcoal">7. Get subset of $M3$</font>

In [23]:
M3[2:end,[1,3]]
Out[23]:
2x2 Array{Float64,2}:
 3.0  0.0
 1.0  1.0
In [24]:
M3[:,1:3]
Out[24]:
3x3 Array{Float64,2}:
 3.0  0.0  3.0
 3.0  0.0  0.0
 1.0  3.0  1.0

<font,color="blue">Learn advanced functions for array by an example</font>

construct an array using comprehensions

In [45]:
x=[1:3]
s=Float64[x[i]^2+1 for i=1:length(x)]
Warning: redefining constant x
Out[45]:
3-element Array{Float64,1}:
  2.0
  5.0
 10.0

elementwise operator

In [46]:
a=[1,2,3]
a.*a     #elemetwise operator
Out[46]:
3-element Array{Int64,1}:
 1
 4
 9

construct matrices with repeated parts

In [55]:
m=[1 2 3]
println(repmat(m,2,3))
println(repeat(m,inner=[2,3]))
[1 2 3 1 2 3 1 2 3
 1 2 3 1 2 3 1 2 3]
[1 1 1 2 2 2 3 3 3
 1 1 1 2 2 2 3 3 3]

Broadcasting

broadcast( ) can save lots of memory.

In [26]:
a=[10.0 1.0]
b=[0.1 0.2;0.3 0.4]
Out[26]:
2x2 Array{Float64,2}:
 0.1  0.2
 0.3  0.4
In [27]:
broadcast(+,a,b)
Out[27]:
2x2 Array{Float64,2}:
 10.1  1.2
 10.3  1.4

<font,color="blue">sparse matrix</font>

In Julia, data structure sparse matrix can be used to reduce computing time and memory when enough elements in the matrix are zeros.

<font,color="charcoal">1. construct a sparse matrix of zeros</font>

In [29]:
spzeros(2,3)
Out[29]:
2x3 sparse matrix with 0 Float64 entries:

<font,color="charcoal">2. construct a identity sparse matrix of zeros</font>

In [31]:
S=speye(2,3)
Out[31]:
2x3 sparse matrix with 2 Float64 entries:
	[1, 1]  =  1.0
	[2, 2]  =  1.0

<font,color="charcoal">3. get index and elements</font>

In [32]:
findnz(S)
Out[32]:
([1,2],[1,2],[1.0,1.0])

<font,color="charcoal">4. convert dense(sparse) matrices to sparse(dense) matrices</font>

In [36]:
D=full(S)
Out[36]:
2x3 Array{Float64,2}:
 1.0  0.0  0.0
 0.0  1.0  0.0
In [37]:
sparse(D)
Out[37]:
2x3 sparse matrix with 2 Float64 entries:
	[1, 1]  =  1.0
	[2, 2]  =  1.0