零填充矩阵
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您将某些内容分配给超出原始大小的元素,Matlab 会自动用零填充。
Matlab automatically pads with zeros if you assign something to an element outside of the original size.
所以,这实际上并没有回答你的问题,但我认为它回答了你给出的用例。假设您想要将较小的矩阵添加到大矩阵的左上角:
这可以避免分配额外的 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:
This avoids allocating an extra 65000 or so doubles which you would have to do if you re-sized small from 16x16 to 256x256.
使用Matlab中的padarray函数。使用帮助了解其参数
Use padarray function in Matlab. Use Help to know it's parameters
首先要做的是创建一个 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.