MATLAB Answers

autofilling a matrix with 0's

1 view (last 30 days)
luc
luc on 15 Aug 2015
Commented: Star Strider on 15 Aug 2015
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 Comments

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 15 Aug 2015
Edited: Star Strider on 15 Aug 2015
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)

  2 Comments

luc
luc on 15 Aug 2015
Great answer! Thanks!
Star Strider
Star Strider on 15 Aug 2015
Thank you!
My pleasure!

Sign in to comment.

More Answers (0)


Translated by