I would like to create a n x m-dimension Matrix of k x k-dimension Matrices (containing zeros).
How can I do that in Julia?
n-d comprehensions are probably easiest:
[zeros(k,k) for i=1:n, j=1:m]
Update: You need to be careful here: presumably you want to fill the array with different matrices of zeros: the other proposed solutions (fill or repmat) will actually give you an array where all the entries are the same zeros matrix, e.g.:
julia> k = 2; n = 3; m = 4; A = fill(zeros(k, k), n, m)
3×4 Array{Array{Float64,2},2}:
[0.0 0.0; 0.0 0.0] [0.0 0.0; 0.0 0.0] [0.0 0.0; 0.0 0.0] [0.0 0.0; 0.0 0.0]
[0.0 0.0; 0.0 0.0] [0.0 0.0; 0.0 0.0] [0.0 0.0; 0.0 0.0] [0.0 0.0; 0.0 0.0]
[0.0 0.0; 0.0 0.0] [0.0 0.0; 0.0 0.0] [0.0 0.0; 0.0 0.0] [0.0 0.0; 0.0 0.0]
julia> A[1,1][1,1] = 1; A
3×4 Array{Array{Float64,2},2}:
[1.0 0.0; 0.0 0.0] [1.0 0.0; 0.0 0.0] [1.0 0.0; 0.0 0.0] [1.0 0.0; 0.0 0.0]
[1.0 0.0; 0.0 0.0] [1.0 0.0; 0.0 0.0] [1.0 0.0; 0.0 0.0] [1.0 0.0; 0.0 0.0]
[1.0 0.0; 0.0 0.0] [1.0 0.0; 0.0 0.0] [1.0 0.0; 0.0 0.0] [1.0 0.0; 0.0 0.0]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With