我从未使用过 mat2cell 来划分矩阵,现在想想这似乎是一个非常好的主意。由于我的计算机上没有 MATLAB,因此我将描述我的做法,其中不涉及 mat2cell。
忽略最后一列和最后一行很容易:
d = 3; % the dimension of the sub matrix
[x,y] = size(f);
% perform integer division by three
m = floor(x/d);
n = floor(y/d);
% find out how many cols and rows have to be left out
m_rest = mod(x,d);
n_rest = mod(y,d);
% remove the rows and columns that won't fit
new_f = f(1:(end-m_rest), 1:(end-n_rest));
% this steps you won't have to perform if you use mat2cell
% creates the matrix with (m,n) pages
new_f = reshape( new_f, [ d m d n ] );
new_f = permute( new_f, [ 1 3 2 4 ] );
现在您可以像这样访问子矩阵:
new_f(:,:,1,1) % returns the 1st one
new_f(:,:,3,2) % returns the one at position [3,2]
如果您想使用 mat2cell 来做到这一点,您可以执行如下操作:
% after creating new_f, instead of the reshape, permute
cells_f = mat2cell(new_f, d*ones(1,m), d*ones(1,n));
然后你可以通过不同的方式访问它:
cells_f{1,1}
cells_f{3,2}
我无法测试单元方法,因为我的电脑上没有 MATLAB,但如果我能正确回忆起 mat2cell 的用法,它应该可以正常工作。
希望能帮助到你 :)