vectorization - Accelerating Iterations- MATLAB -


consider 2 vectors a = [20000000 x 1] , b = [20000000 x 1 ]

i need find sum of corresponding every unique element of b.

although looks easy, taking forever in matlab.

currently, using

u = unique(b); length_u = length(u); c = zeros(length_u,1);  = 1:length_u    c(i,1) = sum(a(b==u(i))); end 

is there anyway make run faster? tried splitting loop , running 2 parfor loops using parallel computing toolbox(because have 2 cores). still takes hours.

p.s: yes, should better computer.

you must see this answer first.
if must, can use combination of histc , accumarray

a = randi( 500, 1, 100000 ); b = randi( 500, 1, 100000 );  ub = unique( b );  [ignore idx] = histc( b, [ub-.5 ub(end)+.5] ); c = accumarray( idx', a' )'; 

see toy comparison naive for-loop implementation on ideone.

how work?

we use second outout of histc map elements of b (and later a) bins defined elements of ub (the unique elements of b).
accumarray used sum entries of a accorind mapping defined idx.
note: assume unique elements of b @ least 0.5 apart.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -