Is there any way to make this code faster?
Mostrar comentarios más antiguos
Hi, I have this code that tosses out pixels from a cc list
for i = 1:cc.NumObjects
for j = length(cc.PixelIdxList{i}):-1:1
ind = cc.PixelIdxList{i}(j);
if ~imdiff(ind) || ~imcombined(ind)
cc.PixelIdxList{i}(j) = [];
end
end
end
imdiff and imcombined are both images. The code at the moment takes quite a while to churn through a decent sized image. Is there any way to make this code run faster?
Respuesta aceptada
Más respuestas (1)
Sean de Wolski
el 17 de Jul. de 2012
Editada: Sean de Wolski
el 17 de Jul. de 2012
That whole thing should be vectorizable assuming imdiff and imcombined (whatever those are) can handle it:
cellfun(@(c)c(imdiff(c)&imcombined(c)),CC.PixelIdxList,'uni',false)
3 comentarios
C.J. Harris
el 17 de Jul. de 2012
Hmm.. is there any point using cellfun here? Does it increase speed or readability? At least in 2006b I find very few circumstances where cellfun gives improved performance over a simple loop.
Sean de Wolski
el 17 de Jul. de 2012
No. I just like it :)
You could keep the outer for-loop just as easily. The point is that the inner for-loop does not need to check each value if imdiff and imcombined are either images or functions that can be passed vector inputs.
Qingyang
el 17 de Jul. de 2012
Categorías
Más información sobre Mathematics en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!