autofilling a matrix with 0's

1 visualizzazione (ultimi 30 giorni)
luc
luc il 15 Ago 2015
Commentato: Star Strider il 15 Ago 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?

Risposta accettata

Star Strider
Star Strider il 15 Ago 2015
Modificato: Star Strider il 15 Ago 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 Commenti
luc
luc il 15 Ago 2015
Great answer! Thanks!
Star Strider
Star Strider il 15 Ago 2015
Thank you!
My pleasure!

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by