fork download
  1. # your code goes here
  2. A = randi( 500, 1, 100000 );
  3. B = randi( 500, 1, 100000 );
  4.  
  5. tic;
  6. ub = unique( B );
  7. toc
  8. tic;
  9. C = zeros(1, numel(ub));
  10.  
  11. for ii = 1:numel(ub)
  12. C(ii) = sum(A(B==ub(ii)));
  13. end
  14. toc
  15.  
  16. tic;
  17. [ignore idx] = histc( B, [ub-.5 ub(end)+.5] );
  18. C2 = accumarray( idx', A' )';
  19. toc
  20. assert( all( C-C2 == 0 ), 'non matching results' );
  21. disp('==============================================')
Success #stdin #stdout 0.58s 65304KB
stdin
Standard input is empty
stdout
Elapsed time is 0.0207201 seconds.
Elapsed time is 0.336658 seconds.
Elapsed time is 0.025557 seconds.
ans =

 Columns 1 through 20:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

 Columns 21 through 40:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

 Columns 41 through 60:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

 Columns 61 through 80:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

 Columns 81 through 100:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

 Columns 101 through 120:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

 Columns 121 through 140:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

 Columns 141 through 160:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

 Columns 161 through 180:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

 Columns 181 through 200:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

 Columns 201 through 220:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

 Columns 221 through 240:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

 Columns 241 through 260:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

 Columns 261 through 280:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

 Columns 281 through 300:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

 Columns 301 through 320:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

 Columns 321 through 340:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

 Columns 341 through 360:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

 Columns 361 through 380:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

 Columns 381 through 400:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

 Columns 401 through 420:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

 Columns 421 through 440:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

 Columns 441 through 460:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

 Columns 461 through 480:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

 Columns 481 through 500:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

==============================================