Zero matrix filling

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

+4
source share
5 answers

Matlab automatically fills in zeros if you assign something to an element outside its original size.

>> A = rand(16, 16); >> A(256, 256) = 0; >> size(A) ans = 256 256 
+9
source
 padded = zeros(256,256); data = rand(16,16); padded(1:16,1:16) = data; 
+3
source

So this does not actually answer your question, but I think it answers the use case you gave. Assuming you want to add a smaller matrix to the upper left corner of the large matrix:

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

This avoids highlighting an additional 65,000 or so doublings, which you will need to do if you resize from 16x16 to 256x256.

+3
source

Use padarray function in matlab. Use Help to find out its parameters.

+3
source

The first thing to do is create a 256x256 output matrix, assuming you want to keep the original 256x256 matrix in its purest form. Then move the 256x256 input matrix values ​​to the output matrix. The next step is to add 16x16 elements to the output matrix.

But before anyone can answer this, you need to explain how the 16x16 matrix relates to the 256x256 matrix. In other words, will the first element (0,0) of the 16x16 matrix be added to the first element (0,0) of the 256x256 matrix? What about the secound (0, 1) element - do you know where this will be added? What about the element (1, 0) of the 16x16 matrix - which element of the 256x256 matrix is ​​added? Once you find out, you can simply encode some loops to add the corresponding 256x256 matrix output element to each element of the 16x16 matrix.

0
source

Source: https://habr.com/ru/post/1342241/


All Articles