How to avoid for loop in specific case

Hi everybody, I would like to avoid for loop in a case shown below. This is a broader tops how to generate vectors from a matrix with start point and end point in two colums.
zn1=find(zn==1);
zn2=zn1(2:end);
zn1=zn1(1:end-1);
znroz=zn2-zn1;
znacky = zn;
for i=1:length(zn1)
if(0.15*fsnew<znroz(i))
znacky(zn1(i):zn2(i))=1;
else
znacky(zn1(i))=0;
end
end

Respuestas (2)

Jan
Jan el 16 de Mayo de 2013
zn1 = find(zn==1);
znDiff = diff(zn1);
znacky = zeros(size(zn));
index = find(znDiff > 0.15 * fsnew);
znacky(zn(index)) = 1;
znacky(zn(index + 1)) = -1;
znacky = cumsum(znacky);
I cannot test this currently, such that some index problems can be expected. But the general idea should be clear.
Andrei Bobrov
Andrei Bobrov el 16 de Mayo de 2013
Editada: Andrei Bobrov el 16 de Mayo de 2013
Used of Jan Simon's idea. [EDIT]
zn1=find(zn==1);
ii = diff(zn1);
o2 = zeros(size(zn));
out = zn;
t = ii > 0.15 * fsnew;
o2(zn1(t)) = 1;
o2(zn1(~t)) = 1 - find(~t);
o2(zn1(end)+1) = - sum(o2);
o2 = (cumsum(o2) > 0);
out(o2) = 1;
out(zn1(~t)) = 0;

La pregunta está cerrada.

Preguntada:

Jan
el 16 de Mayo de 2013

Cerrada:

el 20 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by