matrix padding with zero

조회 수: 2 (최근 30일)
Anvinder
Anvinder 2016년 3월 30일
댓글: Anvinder 2016년 4월 1일
I have a 3x3 matrix and a 2x3 matrix. I need to pad zeros in the 2x3 matrix only at the bottom row such that it becomes 3x3.
If i have another matrix let's say 5x4 and second one 5x2, i need to pad the last 2 columns of the 5x2 matrix such that it has same rows and columns as 5x4.
a = [1,2,3; 3,2,1; 1,2,3];
b = [1,2,3; 3,2,1];
d = padarray(b,[1,0],0)
This code is padding a row above and a row below the matrix but i just want it below the 2x3 matrix.
  댓글 수: 4
Anvinder
Anvinder 2016년 4월 1일
Hi, Apologize for the delayed response.
Here is the picture of the matrices i want to be padded in the given manner.
Image Analyst
Image Analyst 2016년 4월 1일
Yep, my answer below will do it. Simply assign the lower right element to zero, even if that element does not exist yet, and it will pad everything up to there with zeros.

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

채택된 답변

Image Analyst
Image Analyst 2016년 3월 30일
Try this trick:
matrix2x2(3,3) = 0; % Add a row of zeros.
Then to turn the 5x2 into a 5x4:
b = rand(5,2)
array5x4 = padarray(b,[0,2],0, 'post')
  댓글 수: 1
Anvinder
Anvinder 2016년 4월 1일
If i write : a = [1,2,3; 3,2,1; 1,2,3]; b = [1,2,3; 3,2,1]; c = padarray(b,[0,2],0, 'post') then the input, output and padding it does is as per the shown figure attached but this is different from expected.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by