I’m looking for a function to construct a sparse matrix based on several blocks of smaller sparse matrices.

An simple example:

`A11=A12=A21=A22=sprandn(4,2,0.6)`

The final sparse matrix A should be a combination of these 4 blocks like:

A11 A12

A21 A22

`blockdiag()`

doesn’t help directly here since it only returns block diagonal matrix. And the matrices I worked with are pretty large. So I do care about the performance of such operation.

# Construct large sparse matrix by blocks

**WenlongGong**#1

**fredrikekre**#2

Is normal concatenation not good/fast enough?

```
julia> A11 = A12 = A21 = A22 = sprand(4, 4, 0.6);
julia> A = [A11 A12;
A21 A22];
```

**rdeits**#5

`[ ]`

is just helpful syntax for the functions `vcat`

, `hcat`

, and `hvcat`

, so you can just call those yourself:

```
julia> blocks = [eye(2) for _ in 1:6]
julia> hcat(blocks...)
2×12 Array{Float64,2}:
1.0 0.0 1.0 0.0 1.0 0.0 1.0 0.0 1.0 0.0 1.0 0.0
0.0 1.0 0.0 1.0 0.0 1.0 0.0 1.0 0.0 1.0 0.0 1.0
julia> vcat(blocks...)
12×2 Array{Float64,2}:
1.0 0.0
0.0 1.0
1.0 0.0
0.0 1.0
1.0 0.0
0.0 1.0
1.0 0.0
0.0 1.0
1.0 0.0
0.0 1.0
1.0 0.0
0.0 1.0
julia> hvcat((3, 3), blocks...)
4×6 Array{Float64,2}:
1.0 0.0 1.0 0.0 1.0 0.0
0.0 1.0 0.0 1.0 0.0 1.0
1.0 0.0 1.0 0.0 1.0 0.0
0.0 1.0 0.0 1.0 0.0 1.0
julia> hvcat((2, 2, 2), blocks...)
6×4 Array{Float64,2}:
1.0 0.0 1.0 0.0
0.0 1.0 0.0 1.0
1.0 0.0 1.0 0.0
0.0 1.0 0.0 1.0
1.0 0.0 1.0 0.0
0.0 1.0 0.0 1.0
```