How to efficiently create a signal of alternating 0- and 1-intervals with different lenghts?
2 ビュー (過去 30 日間)
古いコメントを表示
I need to create an array of alternating intervals containing a given number of 0s and 1s defined by different distributions and I need to do this efficiently, because the output array can be very long (2^20 or more). So far, I have come up with this code (P1 and P2 are the distributions of the numbers of 1s and 0s and t is usually just a 1:1:max vector):
function I = signal(t,P1,P2);
% transforms distributions of on- and off-periods to intensity time-trace
p1=[];
p=find(P1); %%find non-zero elements
ii=1;
while (ii<=length(p)) %%put all periods in a single array
j=p(ii);
p1=horzcat(p1,t(j)*ones(P1(j),1)');
ii=ii+1;
end;
p=randperm(length(p1)); %%shuffle
p1=p1(p);
p2=[];
p=find(P2); %%the same for the second distribution
ii=1;
while (ii<=length(p))
j=p(ii);
p2=horzcat(p2,t(j)*ones(P2(j),1)');
ii=ii+1;
end;
p=randperm(length(p2));
p2=p2(p);
I=[]; %%merge
for ii=1:length(p1)
I=vertcat(I,ones(p1(ii),1),zeros(p2(ii),1));
end;
It works, but the "merge" part takes very long to execute. Is there a way to do this more efficiently?
0 件のコメント
回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!