Adding zeros to a matrix to match the dimensions of two matrices.
35 次查看(过去 30 天)
显示 更早的评论
Hi ! need help to match the size of two matrices. I have two matrices of dimensions mxn and jxk. I want make mxn of size jxk by adding zeros at the end of the mxn. A of mxn dimenssion and B of jxk dimenssion. Am doing by this way:
newA=[A,zeros(size(B)]
采纳的回答
Guillaume
2017-11-27
newA = [A, zeros(size(A, 1), size(B, 2)-size(A, 2)); zeros(size(B, 1)-size(A, 1), size(B, 2))];
Assuming that both dimensions of B are greater than A.
0 个评论
更多回答(1 个)
James Tursa
2017-11-27
Another way:
newA = zeros(size(B));
newA(1:size(A,1),1:size(A,2)) = A;
2 个评论
Guillaume
2017-11-27
Yes, actually simpler than my answer. And in case A is not of class double:
newA = zeros(size(B), 'like', A);
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!