MATLAB: Indices and Filling Vectors

indicesvectors

Our assignment is to take a vector (t = [1,2,1,2,1,3]) and assign another number with a a length of 5 to another vector. Example 1 = [0,0,0,0,0]. Each number in vector t is to be a length of 5 and so our new vector d should be length 30 total. My code i wrote will only spit out 15.
t = [1,2,1,2,1,3] ;
for n = 1:length(t)
if t(n) == 1
A = zeros(1,5)
elseif t(n) == 2
B = ones(1,5);
else t(n) = 3;
C = zeros(1,5) + 2;
end
end
d = [A B A B A C];
disp(d)

Best Answer

Use repelem or kron:
>> t = [1,2,1,2,1,3];
>> d = kron(t-1,ones(1,5))
d =
0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 2 2 2 2 2