零填充矩阵

发布于 2024-10-20 00:12:21 字数 83 浏览 2 评论 0原文

我有一个 16X16 矩阵。我必须将它添加到 256X256 矩阵中。谁能帮助我如何将这个 16X16 矩阵变成 256X256 并用零填充剩余的矩阵?

I have a 16X16 matrix. I have to add it to a 256X256 matrix. Can anyone help me how to make this 16X16 matrix into 256X256 filling the remaining with zeros?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

寂寞笑我太脆弱 2024-10-27 00:12:21

如果您将某些内容分配给超出原始大小的元素,Matlab 会自动用零填充。

>> A = rand(16, 16);
>> A(256, 256) = 0;
>> size(A)
ans =
   256   256

Matlab automatically pads with zeros if you assign something to an element outside of the original size.

>> A = rand(16, 16);
>> A(256, 256) = 0;
>> size(A)
ans =
   256   256
披肩女神 2024-10-27 00:12:21
padded = zeros(256,256);
data = rand(16,16);
padded(1:16,1:16) = data;
padded = zeros(256,256);
data = rand(16,16);
padded(1:16,1:16) = data;
何以心动 2024-10-27 00:12:21

所以,这实际上并没有回答你的问题,但我认为它回答了你给出的用例。假设您想要将较小的矩阵添加到大矩阵的左上角:

big = ones(256, 256);
small = ones(16, 16);
big(1:16, 1:16) = big(1:16, 1:16) + small;

这可以避免分配额外的 65000 个左右的双倍,如果您将小矩阵从 16x16 调整为 256x256,则必须这样做。

So, this doesn't actually answer your question, but I think it answers the use case you gave. Assuming you want to add the smaller matrix to the upper-left corner of the big matrix:

big = ones(256, 256);
small = ones(16, 16);
big(1:16, 1:16) = big(1:16, 1:16) + small;

This avoids allocating an extra 65000 or so doubles which you would have to do if you re-sized small from 16x16 to 256x256.

滴情不沾 2024-10-27 00:12:21

使用Matlab中的padarray函数。使用帮助了解其参数

Use padarray function in Matlab. Use Help to know it's parameters

禾厶谷欠 2024-10-27 00:12:21

首先要做的是创建一个 256x256 的输出矩阵,假设您想保持原始的 256x256 矩阵不变。接下来,将输入 256x256 矩阵的值移动到输出矩阵中。下一步是将 16x16 元素添加到输出矩阵中。

但是,在任何人真正回答这个问题之前,您需要解释 16x16 矩阵与 256x256 矩阵的关系。换句话说,16x16 矩阵的第一个元素 (0,0) 是否要添加到 256x256 矩阵的第一个元素 (0,0) 中? (0, 1) 的第二个元素怎么样 - 你知道它将被添加到哪里吗? 16x16 矩阵的元素 (1, 0) 怎么样 - 它被添加到 256x256 矩阵的哪个元素中?一旦弄清楚这一点,您应该能够简单地编写一些循环代码,将 16x16 矩阵的每个元素添加到适当的 256x256 输出矩阵元素中。

First thing to do is create an output matrix that is 256x256, assuming you want to keep the original 256x256 matrix pristine. Next, move the values of the input 256x256 matrix into the output matrix. The next step is to add in the 16x16 elements to the output matrix.

But, before anyone can really answer this, you need to explain how the 16x16 matrix relates to the 256x256 matrix. In other words, is the first element (0,0) of the 16x16 matrix going to be added to the first element (0,0) of the 256x256 matrix? How about the secound element of (0, 1) - do you know where that is going to be added in? What about element (1, 0) of the 16x16 matrix - what element of the 256x256 matrix does that get added into? Once you figure this out, you should be able to simply code some loops to add in each element of the 16x16 matrix to the appropriate 256x256 output matrix element.

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