how to velocize it (avoid loop is possible)
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
E=[0 5 6 9 2;0 0 1 3 1;0 5 4 2 4]'
filtro=ones(size(E));
for i=1:width(E)
bu=find(E(:,i),1,'first');
filtro(1:bu,i)=0;
end
filtro
0 Kommentare
Akzeptierte Antwort
Weitere Antworten (1)
Catalytic
am 4 Feb. 2024
I don't know if "velocize" (not a word) is supposed to mean "accelerate" or "vectorize". The two are not the same.
If you're looking for the fastest possible code, there's no way to know in advance because it depends on the sparsity of E. For very dense E, your loop will probably be faster than @Matt J's answer.
E=rand(5000,3000)>0.1;
tic
filtro=true(size(E));
for i=1:width(E)
bu=find(E(:,i),1,'first');
filtro(1:bu,i)=0;
end
toc
tic
[~,I]=max(logical(E),[],1);
filtro=(1:height(E))'>I;
toc
1 Kommentar
Matt J
am 4 Feb. 2024
True, but be mindful of the flipside:
E=rand(5000,3000)>0.7;
tic
filtro=true(size(E));
for i=1:width(E)
bu=find(E(:,i),1,'first');
filtro(1:bu,i)=0;
end
toc
tic
[~,I]=max(logical(E),[],1);
filtro=(1:height(E))'>I;
toc
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements 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!