Concatenation with array of different dimensions

2 visualizzazioni (ultimi 30 giorni)
Del
Del il 23 Set 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]

Risposte (2)

Wayne King
Wayne King il 23 Set 2012
Modificato: Wayne King il 23 Set 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 il 23 Set 2012
Modificato: Andrei Bobrov il 23 Set 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{:});

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by