autofilling a matrix with 0's

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?

 Respuesta aceptada

Star Strider
Star Strider el 15 de Ag. de 2015
Editada: Star Strider el 15 de Ag. de 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 comentarios

luc
luc el 15 de Ag. de 2015
Great answer! Thanks!
Star Strider
Star Strider el 15 de Ag. de 2015
Thank you!
My pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

luc
el 15 de Ag. de 2015

Comentada:

el 15 de Ag. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by