In case if i have the second matrix always smaller than the first one then is there any ways by which it automatically gets padded as per the above mentioned criterion. It seems like i need to manually pad them based on the rows and columns of the second matrix every time.
matrix padding with zero
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
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
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')
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!