Starting with A = [3;4;5]
, I want to duplicate it six times to get A =[3;3;3;3;3;3;4;4;4;4;4;4;5;5;5;5;5;5]
.
I can think of some brute force ways to do it, but looking for something optimized since this will be run through a loop many times.
Starting with A = [3;4;5]
, I want to duplicate it six times to get A =[3;3;3;3;3;3;4;4;4;4;4;4;5;5;5;5;5;5]
.
I can think of some brute force ways to do it, but looking for something optimized since this will be run through a loop many times.
How about using kron
? It's very suited for this purpose.
kron(A,ones(6,1))
Yet another possibility, which involves no arithmetical operations:
reshape(repmat(A,1,6).',[],1);
However, if you really need speed, and if the A
vector size is the same in all iterations of the loop, it's best to precompute (outside the loop) an indexing vector like this
ind = reshape(repmat([1;2;3],1,6).',[],1);
and then within the loop you only need to do
A(ind)
A = [3;4;5];
duplicate_factor = 6;
A = reshape((A*(ones(duplicate_factor,1))')', [], 1)