fork(1) download
  1. num_runs = 5; %// Number of iterations to run benchmarks
  2. for k = 1:50000
  3. tic(); elapsed = toc(); %// Warm up tic/toc
  4. end
  5.  
  6. A = rand(4940);
  7.  
  8.  
  9. sublen = 26; %// subset length
  10. nrows = size(A,1); %// number of rows in input matrix
  11. nsubs = nrows/sublen; %// number of subsets
  12.  
  13. tic
  14. for iter = 1:num_runs
  15. idx1 = bsxfun(@plus,[1:sublen]',[0:sublen-1]*nrows);%//'# starting block indices
  16. idx2 = bsxfun(@plus,idx1(:),[0:nsubs-1]*(nrows*sublen+sublen));%// all block indices
  17. exclude_sum = sum(A(reshape(idx2,sublen,sublen,[])),2); %// block elements summed
  18. %// (these would be subtracted from the wholesome sum)
  19. out = sum(A,2) - exclude_sum(:); %// desired output
  20. end
  21. toc, clear idx1 idx2 exclude_sum out
  22.  
  23. tic
  24. for iter = 1:num_runs
  25. A(kron(eye(nsubs),ones(sublen))==1)=0;
  26. out1 = sum(A,2);
  27. end
  28. toc, clear A out1
Success #stdin #stdout 4.38s 65728KB
stdin
Standard input is empty
stdout
Elapsed time is 0.319547 seconds.
Elapsed time is 2.16733 seconds.