Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- How to vectorize the following block of matlab
- W = zeros( 2, 2, numfoo );
- for i = 1:numfoo
- temp(1:2,1:2) = inv( A(1:2,1:2,i) );
- W(1:2,1:2,i) = ( temp * (temp') );
- end
- Acell = mat2cell(A,2,2,ones(1,1,numfoo));
- temp = cellfun(@inv,Acell,'UniformOutput',0);
- W = cellfun(@(x,y)x*x', temp,'UniformOutput',0);
- W = cell2mat(W);
- dets=A(1,1,:).*A(2,2,:)-A(1,2,:).*A(2,1,:);
- temp=[A(2,2,:)./dets -A(1,2,:)./dets ; -A(2,1,:)./dets A(1,1,:)./dets];
- W=[temp(1,1,:).^2+temp(1,2,:).^2,...
- temp(1,1,:).*temp(2,1,:)+temp(1,2,:).*temp(2,2,:);...
- temp(2,1,:).*temp(1,1,:)+temp(2,2,:).*temp(1,2,:),...
- temp(2,1,:).^2+temp(2,2,:).^2];
- Elapsed time is 1.070547 seconds.
- Elapsed time is 0.012767 seconds.
- clear
- clc
- tic
- numfoo=10000;
- W = zeros( 2, 2, numfoo );
- A=rand(2,2,numfoo);
- for i = 1:numfoo
- temp(1:2,1:2) = inv( A(:,:,i) );
- W(1:2,1:2,i) = temp *temp';
- end
- toc
- tic
- for i = 1:numfoo
- temp(1:2,1:2) = A(:,:,i)[1 0;0 1];
- W(1:2,1:2,i) = temp * temp' ;
- end
- toc
- W = zeros( 2, 2, numfoo );
- for i = 1:numfoo
- temp = inv( A(:,:,i) );
- W(:,:,i) = temp * temp';
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement