function printsum(a)
# summary generates a summary of an object
println(summary(a), ": ", repr(a))
end
# repeat can be useful to expand a grid
m1 = hcat(repeat([1,2], inner=[1], outer=[3*2]),
repeat([1,2,3], inner=[2], outer=[2]),
repeat([1,2,3,4], inner=[3], outer=[1]))
printsum(m1)
# 12×3 Array{Int64,2}: [1 1 1; 2 1 1; 1 2 1; 2 2 2; 1 3 2; 2 3 2; 1 1 3; 2 1 3; 1 2 3; 2 2 4; 1 3 4; 2 3 4]
# 简要介绍hcat的用法
#julia> a = [1; 2; 3; 4; 5]
# 5-element Array{Int64,1}:
# 1
# 2
# 3
# 4
# julia> b = [6 7; 8 9; 10 11; 12 13; 14 15]
# 5×2 Array{Int64,2}:
# 6 7
# 8 9
# 10 11
# 12 13
# 14 15
# julia> hcat(a,b)
# 5×3 Array{Int64,2}:
# 1 6 7
# 2 8 9
# 3 10 11
# 4 12 13
# 5 14 15
#> 12×1 Array{Int64,2}: [1; 2; 1; 2; 1; 2; 1; 2; 1; 2; 1; 2]
# for simple repetitions of arrays:
# using remat
m2 = repmat(m1, 1, 2)
println("size: ", size(m2)) # replicate a9 once into dim1 and twice into dim2
#> size: (12,6)
m3 = repmat(m1, 2, 1)
println("size: ", size(m3)) # replicate a9 twice into dim1 and once into dim2
# 利用julia的推导式创建数组
m4 = [i+j+k for i = 1:2, j = 1:3, k = 1:2] # create a 2*3*2 array of Int64
m5 = ["Hi Im # $(i+2*(j-1 + 3*(k-1)))" for i in 1:2, j in 1:3, k in 1:2]
sum(m4,3) # takes the sum over the third dimension
sum(m4, (1,3)) # sum over first and third dim
maximum(m4,2) # find the max elt along dim 2
minimum(m4,2) # find the min elt along dim 2
println(findmax(m4,3))
# 这里简要介绍findmax的功能:
# julia> A = rand(2,2)
# 2×2 Array{Float64,2}:
# 0.693399 0.0976767
# 0.899617 0.927384
# julia> a = findmax(A,1)
# ([0.899617 0.927384], [2 4])
# julia> typeof(a)
# Tuple{Array{Float64,2},Array{Int64,2}}
m4 = [1 2; 3 4]
println(m4)
m4 = m4 .+ 3 # add 3 to all elements
println(m4)
m4 = m4 .+ [1, 2] # adds vector [1, 2] to all elements along first dim
println(m4)
m4 = m4[:,1]
println(m4)
m4 = rand(1:6,2,2)
printsum(m4)
# try的用法
try
# this will cause an error, you have to assign the correct type
m4[:,1] = rand(2,3)
catch err
println(err)
end
# 下面是一个正确的例子
m4 = [1 2; 3 4]
try
m4[:,1] = [3., 4.]
println(m4)
catch err
println(err)
end