How to efficiently create a signal of alternating 0- and 1-intervals with different lenghts?

2 vues (au cours des 30 derniers jours)
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?

Réponses (0)

Catégories

En savoir plus sur Resizing and Reshaping Matrices 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