zero padding a matrix

后端 未结 5 713
深忆病人
深忆病人 2021-01-13 19:18

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?

5条回答
  •  [愿得一人]
    2021-01-13 19:44

    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.

提交回复
热议问题