3

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.

1

3 Answers 3

4

How about using kron? It's very suited for this purpose.

kron(A,ones(6,1)) 
Sign up to request clarification or add additional context in comments.

Comments

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) 

Comments

0
A = [3;4;5]; duplicate_factor = 6; A = reshape((A*(ones(duplicate_factor,1))')', [], 1) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.