performance - Matlab: Efficiently Generating Subarrays from an array -


i have m x m matrix m sampling different parts of generate k sub-arrays n x n x k matrix n. wondering is: can done efficiently without loop?

here simple example:

m = [1:10]'*[1:10];                 %//' large matrix indxs = [1 2;2 1;2 2]; n = zeros(4,4,3);                   %// matrix contain subarrays  i=1:3,    n(:,:,i) = m(3-indxs(i,1):6-indxs(i,1),3-indxs(i,2):6-indxs(i,2)); end 

in actual code, matrices m , n large , operation looped on thousands of times, inefficiency taking significant toll on run-time.

it can vectorized using bsxfun twice. doesn't mean it's more efficient, though:

m = [1:10].'*[1:10]; %'// large matrix indxs = [1 2;2 1;2 2];  r = size(m,1); ind = bsxfun(@plus, (3:6).', ((3:6)-1)*r); %'// "3" , "6" per example n = m(bsxfun(@minus, ind, reshape(indxs(:,1)+indxs(:,2)*r, 1,1,[]))); 

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -