MATLAB: Convert 16X16 to 256X256

matricesmatrix manipulationzero padding

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?? thank u in advance..

Best Answer

B = zeros(256,256);
B(1:size(A,1),1:size(A,2)) = A; %upper left
B(end-size(A,1)+1:end, 1:size(A,2)) = A; %lower left
With obvious generalizations to upper right and lower right
c = floor((size(B)-size(A))/2);
B(c(1)+1:c(1)+size(A,1), c(2)+1:c(2)+size(A,2)) = A; %center
The centering algorithm biases towards the upper left if the sizes are not both even or not both odd.