autofilling a matrix with 0's
2 次查看(过去 30 天)
显示 更早的评论
Say I have a matrix A=[1 1 1] B=[2 3]
I want to get this C=[1 1 1;2 3 0]
but I want the 0 to be autfilled depending on how long the A matrix is.
So no manual input, but an automatic fill.
Something like this:
C=[A;B 0:0:end]
Any ideas?
0 个评论
采纳的回答
Star Strider
2015-8-15
编辑:Star Strider
2015-8-15
One possibility:
A=[1 1 1];
B=[2 3];
C = zeros(size(B,2), size(A,2));
C(1,:) = A;
C(2,1:size(B,2)) = B;
C =
1 1 1
2 3 0
A function to do this is:
function C = matcreate(varargin)
nrow = nargin;
ncol = 1;
for k1 = 1:nrow
len(k1) = length(varargin{k1});
ncol = max(len(k1), ncol);
end
C = zeros(nrow,ncol);
for k1 = 1:nrow
C(k1,1:len(k1)) = varargin{k1};
end
end
A=[1 1 1];
B=[2 3];
C = matcreate(A,B)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!