필터 지우기
필터 지우기

Generating a number with specific number of times.

조회 수: 2 (최근 30일)
RDG
RDG 2013년 3월 26일
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.

채택된 답변

Image Analyst
Image Analyst 2013년 3월 26일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by