Effacer les filtres
Effacer les filtres

Generating a number with specific number of times.

2 vues (au cours des 30 derniers jours)
RDG
RDG le 26 Mar 2013
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.

Réponse acceptée

Image Analyst
Image Analyst le 26 Mar 2013
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

Plus de réponses (0)

Catégories

En savoir plus sur Random Number Generation dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by