从中心点向外复制矩阵

发布于 2024-12-10 17:13:19 字数 1788 浏览 0 评论 0原文

我对编程很陌生,所以在我完成这项练习时,我正在寻找一些专家来推动我走向正确的方向。

我已经使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉溺在你眼里的海 2024-12-17 17:13:19

如果您的所有矩阵都是第一个矩阵的真正副本(“复制”)(将其称为 mat18),则对于您的第 n 步,您可以调用

largemat=repmat(mat18, 2*n+1, 2*n+1);

例如

largemat=repmat(mat18, 7,7);

生成您在示例中描述的矩阵。正如克莱门特所说,这直接创建了大矩阵,而不是通过扩展现有矩阵。

更新 2

如果您的矩阵不同,例如您有 49 个矩阵存储在 元胞数组 mymatrices{1}, mymatrices{2}, ... mymatrices{49},您可以从

largemat{1}=mymatrices{1};

Now 开始根据 mymatriceslargemat{n 计算下一个 largemat{n+1} },您需要在 largemat{n}“周围”添加下一个 mymatrices。 largemat{n} 已包含第一个 m^2 矩阵,其中 m=2*n+1,因此您需要 mymatrices {(m^2+1):((m+2)^2)} 并正确排列它们:

然后在每个步骤中将其放大

largemat{n+1} = [ largemat{n} cell2mat(mymatrices(m^2+(1:m))')]; %# add to the right
largemat{n+1} = [ largemat{n+1} ; cell2mat(mymatrices(m^2+m+(m+1:-1:1)))]; %# add at bottom
largemat{n+1} = [ cell2mat(mymatrices(m^2+2*m+1+(m+1:-1:1))') largemat{n+1}]; %# add at right
largemat{n+1} = [ cell2mat(mymatrices(m^2+3*m+2+(1:m+2))) ; largemat{n+1}]; %# add at top

要测试此代码,让我们使用简单的子矩阵的数字:

mymatrices=mat2cell(1:49,1,ones(1,49));

现在我们可以运行上面的代码一个循环,打印中间结果来检查:

largemat{1}=mymatrices{1};
for n=1:3
    m=2*n-1;
    largemat{n+1} = [ largemat{n} cell2mat(mymatrices(m^2+(1:m))')]; %# add to the right
    largemat{n+1} = [ largemat{n+1} ; cell2mat(mymatrices(m^2+m+(m+1:-1:1)))]; %# add at bottom
    largemat{n+1} = [ cell2mat(mymatrices(m^2+2*m+1+(m+1:-1:1))') largemat{n+1}]; %# add at right
    largemat{n+1} = [ cell2mat(mymatrices(m^2+3*m+2+(1:m+2))) ; largemat{n+1}]; %# add at top
    largemat{n+1}
end

打印

ans =
     7     8     9
     6     1     2
     5     4     3
ans =
    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
ans =
    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

要测试它也适用于非标量输入矩阵,用作

mymatrices=cell(1,49);
for i=1:49,mymatrices{i}=rand(9,9),end;

输入,不会崩溃...即使我没有手动检查结果矩阵是否正确;-) 。

If all your matrices are truly copies ("replicates") of the initial one (call it mat18), for your nth step you could just call

largemat=repmat(mat18, 2*n+1, 2*n+1);

e.g.

largemat=repmat(mat18, 7,7);

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 by

largemat{1}=mymatrices{1};

Now to calculate the next largemat{n+1} from mymatrices and largemat{n}, you need to add the next mymatrices "around" the largemat{n}. largemat{n} already contains the first m^2 matrices with m=2*n+1, so you will need mymatrices{(m^2+1):((m+2)^2)} and arrange them correctly:

and then in each step enlarge it

largemat{n+1} = [ largemat{n} cell2mat(mymatrices(m^2+(1:m))')]; %# add to the right
largemat{n+1} = [ largemat{n+1} ; cell2mat(mymatrices(m^2+m+(m+1:-1:1)))]; %# add at bottom
largemat{n+1} = [ cell2mat(mymatrices(m^2+2*m+1+(m+1:-1:1))') largemat{n+1}]; %# add at right
largemat{n+1} = [ cell2mat(mymatrices(m^2+3*m+2+(1:m+2))) ; largemat{n+1}]; %# add at top

To test this code, lets use simple numbers for your submatrices:

mymatrices=mat2cell(1:49,1,ones(1,49));

Now we can run the above code in a loop, printing intermediate results to check:

largemat{1}=mymatrices{1};
for n=1:3
    m=2*n-1;
    largemat{n+1} = [ largemat{n} cell2mat(mymatrices(m^2+(1:m))')]; %# add to the right
    largemat{n+1} = [ largemat{n+1} ; cell2mat(mymatrices(m^2+m+(m+1:-1:1)))]; %# add at bottom
    largemat{n+1} = [ cell2mat(mymatrices(m^2+2*m+1+(m+1:-1:1))') largemat{n+1}]; %# add at right
    largemat{n+1} = [ cell2mat(mymatrices(m^2+3*m+2+(1:m+2))) ; largemat{n+1}]; %# add at top
    largemat{n+1}
end

which prints

ans =
     7     8     9
     6     1     2
     5     4     3
ans =
    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
ans =
    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

To test it also works for nonscalar input matrices, use

mymatrices=cell(1,49);
for i=1:49,mymatrices{i}=rand(9,9),end;

as input, which doesn't crash... even though I haven't checked manually that the resulting matrix is correct ;-).

深海里的那抹蓝 2024-12-17 17:13:19

编写一个为给定层数生成所有矩阵的函数可能更简单,而不是尝试用新层扩展现有矩阵。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文