Filter löschen
Filter löschen

Concatenation with array of different dimensions

3 Ansichten (letzte 30 Tage)
Del
Del am 23 Sep. 2012
How do you concatenate arrays with different dimensions?
So for example, if I have a1=[1;2]; a2=[4;1;9]; a3=[5];
and what I wanna produce is the matrix:
A=[1 4 5; 2 1 0; 0 9 0]

Antworten (2)

Wayne King
Wayne King am 23 Sep. 2012
Bearbeitet: Wayne King am 23 Sep. 2012
a1=[1;2]; a2=[4;1;9]; a3=[5];
a1 = padarray(a1,1,'post');
a3 = padarray(a3,2,'post');
A = reshape(cat(1,a1,a2,a3),3,3);

Andrei Bobrov
Andrei Bobrov am 23 Sep. 2012
Bearbeitet: Andrei Bobrov am 23 Sep. 2012
a = {[1;2],[4;1;9],5}
m = numel(a);
k = cellfun(@numel,a);
out = zeros(max(k),m);
for jj = 1:m
out(1:k(jj),jj) = a{jj}(:);
end
or
a = {[1;2],[4;1;9],5};
m = numel(a);
k = cellfun(@numel,a);
i1 = zeros(max(k),m);
i1(k + (0:m-1)*max(k)) = 1;
out = flipud(cumsum(i1(end:-1:1,:)));
out(out>0) = cat(1,a{:});

Kategorien

Mehr zu Creating and Concatenating Matrices 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!

Translated by