For循环将矩阵拆分为相等大小的子矩阵 [英] For loop to split matrix to equal sized sub-matrices

查看:108
本文介绍了For循环将矩阵拆分为相等大小的子矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个大小为400x400的方矩阵,我将如何使用for循环将其拆分为20x20的组成子矩阵?我什至不知道从哪里开始!

Given a square matrix of say size 400x400, how would I go about splitting this into constituent sub-matrices of 20x20 using a for-loop? I can't even think where to begin!

我想我想要类似的东西:

I imagine I want something like :

[x,y] = size(matrix)

for i = 1:20:x
    for j = 1:20:y

但是我不确定我将如何进行.有想法吗?

but I'm unsure how I would proceed. Thoughts?

推荐答案

好吧,我知道张贴者明确要求了for循环,而Jeff Mather的回答正是提供了这一点.

Well, I know that the poster explicitly asked for a for loop, and Jeff Mather's answer provided exactly that.

但是我仍然很好奇是否可以将矩阵分解成给定大小的图块(子矩阵)而无需循环.如果有人也好奇,这是我想出的:

But still I got curious whether it is possible to decompose a matrix into tiles (sub-matrices) of a given size without a loop. In case someone else is curious, too, here's what I have come up with:

T = permute(reshape(permute(reshape(A, size(A, 1), n, []), [2 1 3]), n, m, []), [2 1 3])

将二维数组A转换为三维数组T,其中每个2d切片T(:, :, i)是大小为m x n的图块之一.第三个索引按标准Matlab线性化顺序枚举图块,首先显示图块行.

transforms a two-dimensional array A into a three-dimensional array T, where each 2d slice T(:, :, i) is one of the tiles of size m x n. The third index enumerates the tiles in standard Matlab linearized order, tile rows first.

变体

T = permute(reshape(A, size(A, 1), n, []), [2 1 3]);
T = permute(reshape(T, n, m, [], size(T, 3)), [2 1 3 4]);

T设为一个四维数组,其中T(:, :, i, j)给出二维切片的切片索引为i, j.

makes T a four-dimensional array where T(:, :, i, j) gives the 2d slice with tile indices i, j.

提出这些表达式的感觉有点像解决滑动难题. ;-)

Coming up with these expressions feels a bit like solving a sliding puzzle. ;-)

这篇关于For循环将矩阵拆分为相等大小的子矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆