Padding an array with some size, and the location

조회 수: 4 (최근 30일)
med-sweng
med-sweng 2015년 2월 7일
댓글: Image Analyst 2015년 2월 7일
Say that we have an array, and I wanted to resize that array while keeping its content. Something I thought of is "padding". When I tried that, I noticed that the original content had different locations in the new array. How can I pad they array, while keeping the original array content in their same location?
Thanks.

채택된 답변

Star Strider
Star Strider 2015년 2월 7일
편집: Star Strider 2015년 2월 7일
I am not certain what you want, but this may work:
A = randi(10, 5, 4); % Original Array
B = zeros(10); % Preallocate Larger Array
B(1:size(A,1), 1:size(A,2)) = A; % Create Padded Array
EDIT — It creates ‘A’ in ‘B’ such that the addressing (element references) are the same in both ‘A’ and ‘B’ for the original matrix ‘A’.

추가 답변 (1개)

Image Analyst
Image Analyst 2015년 2월 7일
What did you try? Did you try padarray() in the Image Processing Toolbox?
m = magic(3);
mPadded = padarray(m, [4, 3])
  댓글 수: 2
med-sweng
med-sweng 2015년 2월 7일
Yes, I used padarray().
For instance, for:
a=[1 3; 4 3; 5 3]
When I typed:
padarray(a,[4 4])
I got the following!
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 3 0 0 0 0
0 0 0 0 4 3 0 0 0 0
0 0 0 0 5 3 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Image Analyst
Image Analyst 2015년 2월 7일
OK, glad it worked for you. It took your array and padded around it with zeros just like it was designed to do.
If you want the matrix not to be padded on all sides, but to keep it in the upper left, you can just assign the last element. For example:
b=magic(3);
b(8,6) = 0
b =
8 1 6 0 0 0
3 5 7 0 0 0
4 9 2 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

댓글을 달려면 로그인하십시오.

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by