您的代码似乎有一些问题。如果我要使用双重循环,我会这样做:
M = someNumber;
N = size(big_mat,1);
maxCornerCoord = N-M+1;
nSubMatrices = maxCornerCoord^2;
block_set = cell(nSubMatrices,1);
block_set = zeros(M,M,nSubMatrices);
block_set = zeros(nSubMatrices,M^2);
for y = 1:maxCornerCoord
for x = 1:maxCornerCoord
index = (y - 1) * maxCornerCoord + x;
block_set{index} = big_mat(x:x+M-1, y:y+M-1);
block_set(:,:,index) = big_mat(x:x+M-1, y:y+M-1);
block_set(index,:) = reshape(big_mat(x:x+M-1, y:y+M-1),1,M^2);
endfor
endfor
编辑
我刚刚看到Octave有一个实现了im2col的版本。因此,您可以将双重循环改写为
%# block_set is a M^2-by-nSubMatrices array
block_set = im2col(big_mat,[M,M],'sliding');
%# if you want, you can reshape the result to a M-by-M-by-nSubMatrices array
block_set = reshape(block_set,M,M,[]);
这可能更快,并且节省大量数字树。