Fast Algorithm for Multiple Projections in Matlab -


my problem of performing many low-dimension projections in matlab. have array z has dimensions (n,l,d); these parameters obtained follows. take input array of size n, n = [200, 200], , n = prod(n) = 200*200 = 40,000 , d = numel(n) = 2; is, n number of points in discretisation grid , d dimension of input array (eg image, or plane height map). discretise possible heights (that program output - note height map mention above) l points, l = 32.

for each i = 1:n , j = 1:l, want project vector z(i,j,:) onto unit ball.* @ moment, have following naive code:

z = reshape(z,[n,l,d]); z_norms = norms(z,2,3); = 1:n j = 1:l     z(i,j,:) = z(i,j,:)/max(1,z_norms(i,j)); end end 

the function norms(v,p,dim) takes p norm of matrix v along dimension dim (in case outputting (n,l) matrix).

i have various ideas how improved. 1 idea following:

for = 1:n j = 1:l normsquared = sum(z(i,j,:).^2) if normsquared > 1     z(i,j,:) = z(i,j,:)/sqrt(normsquared) end end end 

note normsquared overwritten each time, it's not taking space. when used on problem, sped process quite lot; however, have tested on problem, , substantially worse - 3 times slower; in fact, takes 2 , half times long calculate normsquared projection in first case!

weirdly, if change sum(z(i,j,:).^2) z(i,j,1)^2 + z(i,j,2)^2 (in case using d = 2), it's faster first (naive) method... if explain me too, that'd great!

if has advice how speed up, i'd appreciative! 90% of program's run time spent on this!


*actually, want project onto lambda times unit ball lambda parameter, unlikely make difference in algorithm - divide z lambda, projection , multiply lambda, for example.

there's no need use parfor when can rewrite double for loop using bsxfun "vectorize" operation:

z = bsxfun(@rdivide,z,max(1,z_norms)) 

the max function vectorizes thresholding of the n-by-l z_norms matrix such values less or equal one. z 3-dimensional n-by-l-by-d array. bsxfun virtually replicates lower dimension z_norms matrix d times across third dimension of z such element-wise division (rdivide) of 2 can performed. result n-by-l-by-d array.

after profiling code, rewriting loops take advantage of matlab's vectorization capabilities should 1 of first things try improve performance.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -