0

I have a vector that has N=1263 entries:

temp=[14, 0.5, ..., 12]

I want to make a vector which repeats entry 1, i.e. 14, 42 times, then entry 2, i.e. 0.5, 42 times and similarly all through the vector. It should produce a vector with size 53046x1.

The following code does the work for a simple case:

F = [1 4 9];
R = [repmat(F(1),[3,1]); repmat(F(2),[3,1]); repmat(F(3),[3,1])]    
R = [1 1 1 4 4 4 9 9 9]

but it is cumbersome when N becomes large. Is there a faster way around this?

Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
Thomas.LRV
  • 23
  • 6

3 Answers3

3

This is exactly what repelem (introduced in R2015a) is for. For your actual problem, you would use:

R = repelem(temp.',42);  %This will repeat each entry of 'temp' 42 times

For the given example,

F = [1 4 9];
R = repelem(F.',3);      %This will repeat each entry of 'F' 3 times

You can also do it with this:

R = ones(42,1)*temp;
R = R(:);
Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
1

Kind of an unusual way of doing this but it works

https://www.mathworks.com/help/matlab/ref/kron.html

All you need to do is include your matrix as well as a ones matrix of the length of the repetition.

R = kron(F(:),ones(42,1));
Durkee
  • 778
  • 6
  • 14
0
R = reshape(repmat(F, 42, 1), [], 1);
Vahe Tshitoyan
  • 1,439
  • 1
  • 11
  • 21