This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on May 3rd, 2012  |  syntax: None  |  size: 1.08 KB  |  views: 27  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to vectorize the following block of matlab
  2. W = zeros( 2, 2, numfoo );
  3. for i = 1:numfoo
  4.     temp(1:2,1:2) = inv( A(1:2,1:2,i) );
  5.     W(1:2,1:2,i) = ( temp *  (temp') );
  6. end
  7.        
  8. Acell = mat2cell(A,2,2,ones(1,1,numfoo));
  9. temp = cellfun(@inv,Acell,'UniformOutput',0);
  10. W = cellfun(@(x,y)x*x', temp,'UniformOutput',0);
  11. W = cell2mat(W);
  12.        
  13. dets=A(1,1,:).*A(2,2,:)-A(1,2,:).*A(2,1,:);
  14. temp=[A(2,2,:)./dets -A(1,2,:)./dets ; -A(2,1,:)./dets A(1,1,:)./dets];
  15. W=[temp(1,1,:).^2+temp(1,2,:).^2,...
  16.    temp(1,1,:).*temp(2,1,:)+temp(1,2,:).*temp(2,2,:);...
  17.    temp(2,1,:).*temp(1,1,:)+temp(2,2,:).*temp(1,2,:),...
  18.    temp(2,1,:).^2+temp(2,2,:).^2];
  19.        
  20. Elapsed time is 1.070547 seconds.
  21. Elapsed time is 0.012767 seconds.
  22.        
  23. clear
  24. clc
  25. tic
  26. numfoo=10000;
  27. W = zeros( 2, 2, numfoo );
  28. A=rand(2,2,numfoo);
  29. for i = 1:numfoo
  30.     temp(1:2,1:2) = inv( A(:,:,i) );
  31.     W(1:2,1:2,i) = temp *temp';
  32. end
  33. toc
  34.  
  35. tic
  36. for i = 1:numfoo
  37.     temp(1:2,1:2) = A(:,:,i)[1 0;0 1];
  38.     W(1:2,1:2,i) = temp * temp' ;
  39. end
  40. toc
  41.        
  42. W = zeros( 2, 2, numfoo );
  43. for i = 1:numfoo
  44.     temp = inv( A(:,:,i) );
  45.     W(:,:,i) = temp *  temp';
  46. end
clone this paste RAW Paste Data