Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
 
 
主题工具 显示模式
旧 2019-12-10, 20:41   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 倍频程:矩阵中的多个子矩阵

我有一个很大的矩阵,我想从中收集子矩阵的集合。如果我的矩阵是NxN,子矩阵大小是MxM,我想收集I=(N - M + 1)^2个子矩阵。换句话说,我想为原始矩阵中的每个元素提供一个MxM子矩阵,该子矩阵可以位于此类矩阵的左上角。

这是我的代码:

for y = 1:I for x = 1:I index = (y - 1) * I + x; block_set(index) = big_mat(x:x+M-1, y:y+M-1) endfor endfor 如果a)错误,并且b)暗示big_mat(x:x+M-1, y:y+M-1)表达式中有某些内容,则可以得到我想要的内容,而无需两个for循环。任何帮助将非常感激



回答:

您的代码中似乎有些错误。如果我要使用双循环,这是我要怎么做:

M = someNumber; N = size(big_mat,1); %# I assume big_mat is square here %# you need different variables for maxCornerCoord and nSubMatrices (your I) %# otherwise, you are going to index outside the image in the loops! maxCornerCoord = N-M+1; nSubMatrices = maxCornerCoord^2; %# if you want a vector of submatrices, you have to use a cell array... block_set = cell(nSubMatrices,1); %# ...or a M-by-M-by-nSubMatrices array... block_set = zeros(M,M,nSubMatrices); %# ...or a nSubMatrices-by-M^2 array block_set = zeros(nSubMatrices,M^2); for y = 1:maxCornerCoord for x = 1:maxCornerCoord index = (y - 1) * maxCornerCoord + x; %# use this line if block_set is a cell array block_set{index} = big_mat(x:x+M-1, y:y+M-1); %# use this line if block_set is a M-by-M-by-nSubMatrices array block_set(:,:,index) = big_mat(x:x+M-1, y:y+M-1); %# use this line if block_set is a nSubMatrices-by-M^2 array block_set(index,:) = reshape(big_mat(x:x+M-1, y:y+M-1),1,M^2); endfor endfor 编辑

我刚刚看到Oct2有一个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,[]); 这可能更快,并且可以节省大量的数字树。



更多&回答...
poster 当前离线   回复时引用此帖
 


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 06:46


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.