How I can zero pad a 14*14 matrix to 682*577 dimension?

7 ビュー (過去 30 日間)
mohammad nemat
mohammad nemat 2021 年 5 月 9 日
コメント済み: mohammad nemat 2021 年 5 月 9 日
Hi,
I want to zero pad a matrix with 14*14 dimension to have 682*577 dimensions. I want to data be in center of the new matrix. How I can do it?
I used this
D = padarray(h,[334 281],0,'both');
but I get a matrix with 682*576 dimensions. What's wrong?
  3 件のコメント
mohammad nemat
mohammad nemat 2021 年 5 月 9 日
no it's not zero .and i have problem with this dimension .
Matt J
Matt J 2021 年 5 月 9 日
The dimensions seem fine, since
[334 281]*2+14
ans = 1×2
682 576
And, we can check right here and now that if h is not zero, it works:
D = padarray(ones(14),[334 281],0,'both');
imshow(D)

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2021 年 5 月 9 日
編集済み: Image Analyst 2021 年 5 月 9 日
Try this trick
h = ones(14, 14); % Original matrix is 14 x 14
% Pad it out with 334 rows top and bottom,
% and 281 columns left and right.
D = padarray(h, [334, 281], 0, 'both');
whos D % Originally 682x576
% Not the size we want yet. We want 682x577
% Here comes the "trick" to expand out the lower right corner with zeros.
D(end, 577) = 0;
whos D % Now 682x577
% Alternatively you can call padarray() with the 'pre' and 'post' option if you want to use different amounts of padding on each side.
  4 件のコメント
Image Analyst
Image Analyst 2021 年 5 月 9 日
I don't know how that could be. That matrix is not padded with zeros at all.
Attach your h matrix in a .mat file with the paper clip icon.
save('answers.mat', 'h');
Also attach the actual code you used.
mohammad nemat
mohammad nemat 2021 年 5 月 9 日
thanks it's ok.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 5 月 9 日
but I get a matrix with 682*576 dimensions. What's wrong?
You started with a matrix with even dimension, 14 x 14. You used padarray() with 'both'. That puts the same amount of space on the left and right, so the number of columns is going to be N + 14 + N = (2*N) + 14 for some integer N. But that is always going to be an even number. Therefore you cannot use padarray() 'both' to turn an array with even dimension into an array with odd dimension in a single step.

カテゴリ

Help Center および File ExchangeComputer Vision with Simulink についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by