简体   繁体   中英

Efficient concatenation of large number of arrays in Julia

I am using Julia to build a large N x N matrix. It basically consists of repeating the same array:

 L = [1 0; 1 0; 0 0; 0 0]

I basically want this to be repeated N times, vertically and horizontally, for an 2N x 4N array for some general L. The problem is that, when I use hcat, it's very inconvenient, as I believe that I have to put L in N number of times:

cat(dims=2,L,L,L,L,L,L,L,...)

I also didn't have much luck with hvcat . What is the easiest (and most efficient) way to build this large array from L? I would like something as fast as possible, as this a small portion of a much larger code. I expect N to be of the order of 100 or larger.

You can use the repeat function:

L = [1 0; 1 0; 0 0; 0 0];
N = 3;
repeat(L; outer=(N, N));

Output

julia> repeat(L; outer=(N, N))
12×6 Array{Int64,2}:
 1  0  1  0  1  0
 1  0  1  0  1  0
 0  0  0  0  0  0
 0  0  0  0  0  0
 1  0  1  0  1  0
 1  0  1  0  1  0
 0  0  0  0  0  0
 0  0  0  0  0  0
 1  0  1  0  1  0
 1  0  1  0  1  0
 0  0  0  0  0  0
 0  0  0  0  0  0

I assume that you meant that you want a 4N-by-2N matrix, not 2N-by-4N. There is also an inner keyword you can use, which repeats each value instead of the whole input array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM