matlab - Exclude matrix elements from calculation with respect to performance -


i trying save calculation time. doing image processing known lucas kanade algorithm. starting point paper baker / simon.

i doing matlab , use background substractor. want substractor set background 0 or have logical mask 1 foreground , 0 background.

what want have exclude matrix elements background calculation. goal save time calculation. aware can use syntax like

a(a>0) = ...  

but doesn't work in way like

b(a>0) = a.*c.*d 

because getting error:

in assignment a(i) = b, number of elements in b , must same.

this because a,b , c have more elements matrix a.

in c-code loop matrix , check if pixel has value 0 , continue. in case save whole bunch of calculations.

in matlab it's not fast loop through matrix. there fast way solve problem? couldn't find sufficient answere problem here.

i case interested: trying use robust error function instead of quadratic ones.

update:

i tried following approach test speed suggested @acorbe:

function matrixtest() n = 100; = rand(n,n); b = rand(n,n); c = rand(n,n); d = rand(n,n);  profile clear, profile on; i=1:10000         tests(a,b,c,d);   end profile off, profile report;  function result = tests(a,b,c,d)     idx = (b>0);      t = a(idx).*b(idx).*c(idx).*d(idx);     lgs1a(idx) = t;         lgs1b = a.*b.*c.*d; 

and got folloing results profiler of matlab:

t = a(idx).*b(idx).*c(idx).*d(idx); 1.520 seconds  lgs1a(idx) = t;   0.513 seconds idx = (b>0);      0.264 seconds lgs1b = a.*b.*c.*d; 0.155 seconds 

as can see, overhead of accessing matrix index hast far more costs

what following?

 mask = a>0;   b = zeros(size(a));    % # initialization   t = a.*c.*d;  b( mask ) = t( mask ); 

in way select needed elements of t. maybe there overhead in calculation, although negligible respect loops slowness.


edit:

if want more speed, can try more selective approach uses mask everywhere.

 t = a(mask).*c(mask).*d(mask);  b( mask ) = t; 

Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -