我有一个矩阵,它的维数不是3的倍数,也可能是。我们如何将整个图像分成3*3个矩阵的块。(可以忽略不在3*3倍数内的最后一个。此外,3*3矩阵可以保存在数组中。
a=3; b=3; %window size
x=size(f,1)/a; y=size(f,2)/b; %f is the original image
m=a*ones(1,x); n=b*ones(1,y);
I=mat2cell(f,m,n);发布于 2012-04-02 19:26:32
我从来没有用过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}cell方法我不能测试,因为我在这台电脑上没有MATLAB,但如果我能正确地回忆起mat2cell的用法,它应该工作得很好。
希望它能有所帮助:)
https://stackoverflow.com/questions/9972684
复制相似问题