Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. A = rand(4,5,3);
  2. B = rand(4,5,6)
  3.  
  4. corr2(A(:,:,1),B(:,:,1))
  5. corr2(A(:,:,1),B(:,:,2))
  6. corr2(A(:,:,1),B(:,:,3))
  7. ...
  8. corr2(A(:,:,1),B(:,:,6))
  9. ...
  10. corr2(A(:,:,2),B(:,:,1))
  11. corr2(A(:,:,2),B(:,:,2))
  12. ...
  13. corr2(A(:,:,3),B(:,:,6))
  14.  
  15. szA = size(A);
  16. szB = size(B);
  17.  
  18. a1 = bsxfun(@minus,A,mean(mean(A)));
  19. b1 = bsxfun(@minus,B,mean(mean(B)));
  20.  
  21. sa1 = sum(sum(a1.*a1));
  22. sb1 = sum(sum(b1.*b1));
  23.  
  24. v1 = reshape(b1,[],szB(3)).'*reshape(a1,[],szA(3));
  25. v2 = sqrt(sb1(:)*sa1(:).');
  26.  
  27. corr3_out = v1./v2; %// desired output
  28.  
  29. szA = size(A);
  30. szB = size(B);
  31. dim12 = szA(1)*szA(2);
  32.  
  33. a1 = bsxfun(@minus,A,mean(reshape(A,dim12,1,[])));
  34. b1 = bsxfun(@minus,B,mean(reshape(B,dim12,1,[])));
  35.  
  36. v1 = reshape(b1,[],szB(3)).'*reshape(a1,[],szA(3));
  37. v2 = sqrt(sum(reshape(b1.*b1,dim12,[])).'*sum(reshape(a1.*a1,dim12,[])));
  38.  
  39. corr3_out = v1./v2; %// desired output
  40.  
  41. %// Create random input arrays
  42. N = 55; %// datasize scaling factor
  43. A = rand(4*N,5*N,3*N);
  44. B = rand(4*N,5*N,6*N);
  45.  
  46. %// Warm up tic/toc
  47. for k = 1:50000
  48. tic(); elapsed = toc();
  49. end
  50.  
  51. %// Run vectorized and loopy approach codes on the input arrays
  52.  
  53. %// 1. Vectorized approach
  54. %//... solution code (Approach #2) posted earlier
  55. %// clear variables used
  56.  
  57. %// 2. Loopy approach
  58. tic
  59. s_A=size(A,3);
  60. s_B=size(B,3);
  61. out1 = zeros(s_B,s_A);
  62. for ii=1:s_A
  63. for jj=1:s_B
  64. out1(jj,ii)=corr2(A(:,:,ii),B(:,:,jj));
  65. end
  66. end
  67. toc
  68.  
  69. -------------------------- With BSXFUN vectorized solution
  70. Elapsed time is 1.231230 seconds.
  71. -------------------------- With loopy approach
  72. Elapsed time is 139.934719 seconds.
  73.  
  74. A = rand(4,5,1000);
  75. B = rand(4,5,200);
  76. s_A=size(A,3);
  77. s_B=size(B,3);
  78.  
  79. %%% option 1
  80. tic
  81. corr_AB=cell2mat(arrayfun(@(indx1) arrayfun(@(indx2) corr2(A(:,:,indx1),B(:,:,indx2)),1:s_B),1:s_A,'UniformOutput',false));
  82. toc
  83.  
  84. %%% option 2
  85. tic
  86. indx1=repmat(1:s_A,s_B,1);
  87. indx1=indx1(:);
  88. indx2=repmat(1:s_B,1,s_A);
  89. indx2=indx2(:);
  90. indx=[indx1,indx2];
  91.  
  92. corr_AB=arrayfun(@(i) corr2(A(:,:,indx(i,1)),B(:,:,indx(i,2))),1:size(indx,1));
  93. toc
  94.  
  95. %%% option 3
  96. tic
  97. a=1;
  98. for i=1:s_A
  99. for j=1:s_B
  100. corr_AB(a)=corr2(A(:,:,i),B(:,:,j));
  101. a=a+1;
  102. end
  103. end
  104. toc
  105.  
  106. Elapsed time is 9.655696 seconds.
  107. Elapsed time is 9.398979 seconds.
  108. Elapsed time is 8.489744 seconds.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement