从中心点向外复制矩阵
我对编程很陌生,所以在我完成这项练习时,我正在寻找一些专家来推动我走向正确的方向。
我已经使用 horzcat 和 vertcat 手动执行此操作,但我问是否有人知道更简单的方法。
矩阵 1 为 18x18,水平索引从 1 开始。
矩阵 1 位于中心,表示离散空间。矩阵 2,3,4,5,6,7,8,9 是矩阵 1 的复制,也表示离散空间,位于右下、右下、下、左下、左、左上、上和右上。
矩阵 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 也是矩阵 1 的复制品,并围绕矩阵 2 至 9。构建在一起的矩阵形成如下所示的图案。每个矩阵本身就是一个离散单元。
如果您定义了所需的矩阵数量,那么在本例中我们可以说“49”或“从矩阵 1 开始 4 步”(一个人可以从对角线上的 1、9、25、49 或 1、8、23、 46 在一条直线上),如何构建这种模式的矩阵?
43 44 45 46 47 48 49
42 21 22 23 24 25 26
41 20 7 8 9 10 27
40 19 6 1 2 11 28
39 18 5 4 3 12 29
38 17 16 15 14 13 30
37 36 35 34 33 32 31
如果我用图表来演示这一点,我希望我能更好地表达我的问题。
所以第一环是这样:
7 8 9
6 1 2
5 4 3
第二环是这样:
21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13
第三环是这样,
43 44 45 46 47 48 49
42 21 22 23 24 25 26
41 20 7 8 9 10 27
40 19 6 1 2 11 28
39 18 5 4 3 12 29
38 17 16 15 14 13 30
37 36 35 34 33 32 31
依此类推。我希望能够无限地延续这种模式。
在 Matlab 中执行此操作的最佳方法是什么?
我为我的不清楚表示歉意,并需要更好地表达我的问题。您的所有评论对于向我展示如何编写正确的问题非常有用。
非常感谢你们的帮助。刚刚加入这个论坛,因为这里的支持非常出色。
I'm quite new to programming so am looking for some experts to nudge me in the right direction as I complete this exercise.
I have used horzcat and vertcat to manually do this but I'm asking if anyone knows a simpler way.
Matrix 1 is 18x18 and indexed horizontally starting at 1.
Matrix 1 is at the centre and represents a discrete space. Matrix 2,3,4,5,6,7,8,9 are replicates of matrix 1, also represent discrete spaces and are located right, lower right, below, lower left, left, top left, above and top right.
Matrix 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 are also replicates of matrix one and surround Matrix 2 to 9. The matrices built together form a pattern as below. Each matrix is itself a discrete unit.
If you define the number of matrices you require, so let's say in this case '49' or '4 steps from matrix 1' (a person can jump from 1, 9, 25, 49 on a diagonal or 1, 8, 23,46 on a straight line), how do I build the matrices in this pattern?
43 44 45 46 47 48 49
42 21 22 23 24 25 26
41 20 7 8 9 10 27
40 19 6 1 2 11 28
39 18 5 4 3 12 29
38 17 16 15 14 13 30
37 36 35 34 33 32 31
If I demonstrate this diagrammatically, I hope I can express my problem better.
So the first ring is this:
7 8 9
6 1 2
5 4 3
the second ring is this:
21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13
the third ring is
43 44 45 46 47 48 49
42 21 22 23 24 25 26
41 20 7 8 9 10 27
40 19 6 1 2 11 28
39 18 5 4 3 12 29
38 17 16 15 14 13 30
37 36 35 34 33 32 31
and so on. I'd like to be able to continue this pattern infinitely.
What is the best way to do this in Matlab?
I apologise for my lack of clarity and need to express my problem better. All your comments are very useful in showing me how to write a proper problem.
Many thanks for your help guys. Just joined this forum as the support here is exceptional.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的所有矩阵都是第一个矩阵的真正副本(“复制”)(将其称为
mat18
),则对于您的第n
步,您可以调用例如
生成您在示例中描述的矩阵。正如克莱门特所说,这直接创建了大矩阵,而不是通过扩展现有矩阵。
更新 2
如果您的矩阵不同,例如您有 49 个矩阵存储在 元胞数组
mymatrices{1}
,mymatrices{2}
, ...mymatrices{49}
,您可以从Now 开始根据
mymatrices
和largemat{n 计算下一个
,您需要在largemat{n+1}
}largemat{n}
“周围”添加下一个 mymatrices。largemat{n}
已包含第一个m^2
矩阵,其中m=2*n+1
,因此您需要mymatrices {(m^2+1):((m+2)^2)}
并正确排列它们:然后在每个步骤中将其放大
要测试此代码,让我们使用简单的子矩阵的数字:
现在我们可以运行上面的代码一个循环,打印中间结果来检查:
打印
要测试它也适用于非标量输入矩阵,用作
输入,不会崩溃...即使我没有手动检查结果矩阵是否正确;-) 。
If all your matrices are truly copies ("replicates") of the initial one (call it
mat18
), for yourn
th step you could just calle.g.
to produce the matrix you described in your example. As Clement says, this creates the large matrix directly, not by expanding an existing one.
Update 2
If your matrices are different, for example you have 49 matrices stored in a cell array
mymatrices{1}
,mymatrices{2}
, ...mymatrices{49}
, you would start byNow to calculate the next
largemat{n+1}
frommymatrices
andlargemat{n}
, you need to add the next mymatrices "around" thelargemat{n}
.largemat{n}
already contains the firstm^2
matrices withm=2*n+1
, so you will needmymatrices{(m^2+1):((m+2)^2)}
and arrange them correctly:and then in each step enlarge it
To test this code, lets use simple numbers for your submatrices:
Now we can run the above code in a loop, printing intermediate results to check:
which prints
To test it also works for nonscalar input matrices, use
as input, which doesn't crash... even though I haven't checked manually that the resulting matrix is correct ;-).
编写一个为给定层数生成所有矩阵的函数可能更简单,而不是尝试用新层扩展现有矩阵。
It's probably simpler to write a function that generate all your matrix for a given number of layers instead of trying to expand an existing matrix with a new layer.