El miércoles, 3 de junio de 2015, 19:07:19 (UTC+2), Marc Gallant escribió:
>
> If I have an array of square matrices of different sizes; e.g.,
>
> 3-element Array{Array{Float64,2},1}:
> 2x2 Array{Float64,2}:
> 0.539932 0.429322
> 0.623487 0.0397795
> 2x2 Array{Float64,2}:
> 0.35508 0.700551
> 0.768214 0.954056
> 3x3 Array{Float64,2}:
> 0.953354 0.453831 0.991583
> 0.159975 0.116518 0.355275
> 0.791447 0.0104295 0.151609
>
>
> Where the number of elements may be much larger than 3 (e.g., 1000), how
> do I construct a block diagonal matrix, where the blocks are the matrices
> in the array? For the array above, this would be
>
> 7x7 Array{Float64,2}:
> 0.539932 0.429322 0.0 0.0 0.0 0.0 0.0
> 0.623487 0.0397795 0.0 0.0 0.0 0.0 0.0
> 0.0 0.0 0.35508 0.700551 0.0 0.0 0.0
> 0.0 0.0 0.768214 0.954056 0.0 0.0 0.0
> 0.0 0.0 0.0 0.0 0.953354 0.453831 0.991583
> 0.0 0.0 0.0 0.0 0.159975 0.116518 0.355275
> 0.0 0.0 0.0 0.0 0.791447 0.0104295 0.151609
>
>
One way would be to create a matrix of zeros and just insert the non-zero
elements in the correct places, e.g.
julia> A = [ 0.539932 0.429322;
0.623487 0.0397795]
julia> M = zeros(4,4)
4x4 Array{Float64,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
julia> M[1:2, 1:2] = A
2x2 Array{Float64,2}:
0.539932 0.429322
0.623487 0.0397795
julia> M[3:4, 3:4] = A^2
2x2 Array{Float64,2}:
0.559203 0.248883
0.361443 0.269259
julia> M
4x4 Array{Float64,2}:
0.539932 0.429322 0.0 0.0
0.623487 0.0397795 0.0 0.0
0.0 0.0 0.559203 0.248883
0.0 0.0 0.361443 0.269259
It would not be too hard to write a routine to take in the vector of
matrices and keep track automatically
of where they should be inserted.