MATLAB: Generating a number with specific number of times.

cell arrayrandom number generator

Is there any better way to perform these in matlab? eg:
i) Generating a number with specific number of times.
a=[1 2 3 4 5]; %The data
b=cell(10,1);
for i=1:3 %Each data will appear a specific number of times
b{i}=a(1);
end
for j=1:1
b{j+i}=a(2);
end
for k=1:2
b{k+j+i}=a(3);
end
for l=1:1
b{l+k+j+i}=a(4);
end
for m=1:3
b{m+l+k+j+i}=a(5);
end
c=b; %Store the processed data into a variable.

Best Answer

No need to make it complicated with a cell array when a normal numerical array will work just fine:
a=[1 2 3 4 5];
counts = [3, 1, 2, 1, 3]
b = [];
for k = 1 : length(a)
b = [b, a(k) * ones(1, counts(k))];
end
b
Results in command window
b =
1 1 1 2 3 3 4 5 5 5