fork download
  1. A = rand( 500 ) > .5;
  2.  
  3. t=zeros(1,2);
  4. for ii=1:10
  5. xx = randi( 500, 1, 1000 );
  6. yy = randi( 500, 1, 1000 );
  7.  
  8. tic;
  9. s1 = sum( A( sub2ind( size(A), yy, xx ) ) == 0 );
  10. t(1) = t(1)+toc;
  11.  
  12. tic;
  13. s2 = sum( A( [1 size(A,1)] * ( [yy; xx] - 1 ) + 1 ) == 0 );
  14. t(2) = t(2)+toc;
  15. assert(s1==s2);
  16. end
  17. t
Success #stdin #stdout 0.22s 65216KB
stdin
Standard input is empty
stdout
t =

   0.042526   0.017837