# pad matrix to a particular size

조회 수: 140(최근 30일)
Elysi Cochin 2021년 5월 13일
댓글: Hamza Mehmood 2022년 8월 27일
I have a matrix M of size m x n
m and n may or may not be equal - M can be rectangular or square matrix - where m and n are less than 150,
I wanted to resize M to 150 x 150 by padding border with zeros, such that the m x n will come in the center
how to compute value for x and y such that i can use the same line of code for any matrix,
or is there any another way to do?

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

### 채택된 답변

Image Analyst 2021년 5월 13일
You can use the 'pre' and 'post' options to pad each side with the desired number of zeros.
Or use this "trick"
g = ones(150, 150); % Original matrix is 150 x 150
[rows, columns] = size(g);
m = 153
n = 152;
rowsPre = floor((m - rows)/2)
collsPre = floor((n - columns)/2)
% Pad it out with 334 rows top and bottom,
% and 281 columns left and right.
P = padarray(g, [rowsPre, collsPre], 0, 'both');
whos P % Originally 152x152 padded with zeros all around.
% Not the size we want yet. We want 153x152
% Here comes the "trick" to expand out the lower right corner with zeros.
P(m, n) = 0;
whos P % Now 153 x 152
##### 댓글 수: 3표시 이전 댓글 수: 2숨기기 이전 댓글 수: 2
Hamza Mehmood 2022년 8월 27일
Yeah that worked and results prove that padding the ROI to the feature activation dimension may not be the best way to compare using ssim.
Ill be instead reversing the process, cropping the 3D feature activation matrix down to 25x25x25 to ROI 3D coordinates and then compare using ssim.
Thank You !
Much Appreciated.

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

### 추가 답변(1개)

David Hill 2021년 5월 13일
assuming m and n are both even.
##### 댓글 수: 1표시 없음숨기기 없음
Elysi Cochin 2021년 5월 13일
편집: Elysi Cochin 2021년 5월 13일
what if both are odd or any one value is odd
say 71 x 100
or 97 x 97
or 63 x 89

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

### 범주

Find more on Image Quality in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by