In [1]:
using Dates, LinearAlgebra
include("printmat.jl") #function for prettier matrix printing
Out[1]:
and it sometimes matters.
Julia has both vectors and Nx1 arrays (the latter being a special case of NxM arrays). They can often be used interchangeably, but not always (see below for an example).
In particular, you typically use a vector when you want to pull out particular rows from a larger array.
In [2]:
v = ones(Int,2) #a vector with two elements
v2 = ones(Int,2,1) #a 2x1 matrix (Array)
println("v and v2 look similar, but they have different sizes: ")
printmat(v)
printmat(v2)
println("size of v and v2: ",size(v)," ",size(v2))
x = [11 12;21 22;31 32]
println("\nx: ")
printmat(x)
println("x[v,:] is")
printmat(x[v,:]) #instead, x[v2,:] gives an 2x1x2 array
In [3]:
y = [1;2] .+ 1 #do not forget the dot
printmat(y)
In [4]:
for i = 1:5
global Tor #without this, Tor is not seen outside the loop
Tor = cos(i)
end
println(Tor)
Oden = Inf
for i = 1:5
#global Oden #only needed in REPL/scripts
Oden = cos(i) #will overwrite an existing value
end
println("Oden: ",Oden)
In [5]:
function f2(N)
v = falses(N+1)
x = zeros(Int,N,N)
Threads.@threads for i = 1:N
#local v #comment out to see the problem
v = falses(N)
v[i] = true
x[v,i] .= i
end
return x
end
println("this should always be zero. Run a few times.")
M = 100
dev = zeros(M)
for i = 1:M
dev[i] = maximum(abs,f2(i) - diagm(1:i))
end
println(dev)
To create a 'cell array' (a heterogeneous Array), use [x1,x2,...]
Alternatively, you can preallocate as in B = Array{Any}(undef,3) and then fill by, for instance, B[1] = [11 12]
In [6]:
A = [[11 12;21 22],"A nice dog",27]
println("\nThe array A: ")
foreach(i->printmat(A[i]),1:length(A)) #print each element of A
B = Array{Any}(undef,3)
B[1] = [11 12]
B[2] = "A bad cat"
B[3] = pi
println("\nThe array B: ")
foreach(i->printmat(A[i]),1:length(A))
In [7]:
x = [zeros(2,2) for i=1:2] #a vector of two matrices
x[1][1,1] = -99
println("x[1]")
printmat(x[1])
println("x[2]")
printmat(x[2])
In [8]:
A = [1,2]
B = A #A and B are the same
C = A .+ 0 #A and C are not the same
println("old A,B,C (each is a column): ")
printmat([A B C])
A[2] = -999
println("after changing element A[2] to -999, A,B,C are:")
printmat([A B C])
printblue("\nNotice that B changed, but C did not")
In [9]:
A = [1 2]
println("original A: ")
printmat(A)
B = reshape(A,2,1)
C = vec(A)
D = A'
E = fill(A,2)
println("old B, C and D (each is a column): ")
printmat([B C D])
A[2] = -999
println("B, C and D after changing element A[2] to -999")
printmat([B C D])
println("E[1] and E[2] after changing element A[2] to -999")
printmat(E[1])
printmat(E[2])
printblue("\nNotice that B, C, D and E also changed")
When you use an array as a function argument, then that is passed as a reference to the function.
This means that if you change some elements of the array (A[1] = A[1]/2, say) inside the function, then it will also affect the array outside the function (even if they have different names).
In contrast, if you change the entire array (A/2, say) inside the function, then that does not affect the array outside the function.
This applies to arrays, but not to scalars or strings.
If you really need an independent copy of an array, create it by
B = copy(A)
In [10]:
function f1(A)
A[1] = A[1]/2 #changes ELEMENTS of A, affects outside value
return A
end
function f2(A)
A = A/2 #changes all of A, does not affect outside value
return A
end
x = [1.0 2.0]
printlnPs("original x: ",x)
y1 = f1(x)
printlnPs("x (outside function) after calling f1(x): ",x)
x = [1.0 2.0]
printlnPs("\noriginal x: ",x)
y2 = f2(x)
printlnPs("x (outside function) after calling f2(x): ",x)
printblue("\nNotice that f1() changed x also outside the function, but f2() did not")
In [11]:
a = [11 12;21 22]
A = [a,"A nice dog",27] #a heterogeneous array
println("A:")
foreach(i->printmat(A[i]),1:length(A))
printblue("\nChange a[1,1] to -999 and notice that also A changes")
a[1,1] = -999
println("A:")
foreach(i->printmat(A[i]),1:length(A))
B = pushfirst!(A,a)
printblue("\nChange a[1,1] to 123 and notice that also A changes")
a[1,1] = 123
foreach(i->printmat(A[i]),1:length(A))
In [ ]: