Filter löschen
Filter löschen

Generating a number with specific number of times.

2 Ansichten (letzte 30 Tage)
RDG
RDG am 26 Mär. 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.

Akzeptierte Antwort

Image Analyst
Image Analyst am 26 Mär. 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

Weitere Antworten (0)

Kategorien

Mehr zu Random Number Generation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by