Reputation: 673
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.
Upvotes: 1
Views: 140
Reputation: 12653
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.
Upvotes: 4