Divakar's solution is one way to do it, and the link he referenced shows some great ways to duplicate an array. That post, however, is asking to do it without the built-in function repmat
, which is the easiest solution. Because there is no such restriction for you here, I will recommend this approach. Basically, you can use repmat
to do this for you. You would keep the amount of columns the same, and you would duplicate for as many rows as you want. In other words:
myVelDup = repmat(myVel, N, 1);
Example:
myVel = [1 2 3 4 5 6];
N = 4;
myVelDup = repmat(myVel, N, 1);
Output:
>> myVel
myVel =
1 2 3 4 5 6
>> myVelDup
myVelDup =
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
In general, repmat
is called in the following way:
out = repmat(in, M, N);
in
would be a matrix or vector of values you want duplicated, and you would want to duplicate this M
times horizontally (rows) and N
times vertically (columns). As such, for your case, as you have an array, you will want to duplicate this N
times vertically and so we set the first parameter to N
. The second parameter, the columns stay the same so we specify this to be 1 as we don't want to have any duplications... and thus the call to repmat
you see above.
For more information on repmat
, check out this link: http://www.mathworks.com/help/matlab/ref/repmat.html