autofilling a matrix with 0's
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
luc
am 15 Aug. 2015
Kommentiert: Star Strider
am 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 Kommentare
Akzeptierte Antwort
Star Strider
am 15 Aug. 2015
Bearbeitet: Star Strider
am 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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!