How to vectorize this code to eliminate nested For loops
Ältere Kommentare anzeigen
Would like to know how this code be vectorized:
for y=1:rows
for x=1:cols
if (segMat(y,x) == 255)
Energy(y,x) = Energy(y,x) + motionMap(y,x);
end
end
for y=1:rows
for x=1:cols
delta(y,x) = kronDel(255, segMat(y,x));
end
end
The kronDel function acts like the Kronecker delta.
2 Kommentare
Rik
am 4 Sep. 2018
Is segMat a function or an array? The second set of loops could be replaced with a call to arrayfun, but this is not always a speed improvement.
Jae Min Lee
am 4 Sep. 2018
Bearbeitet: Jae Min Lee
am 4 Sep. 2018
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!