Concatenation with array of different dimensions

3 次查看(过去 30 天)
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]

回答(2 个)

Wayne King
Wayne King 2012-9-23
编辑:Wayne King 2012-9-23
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 2012-9-23
编辑:Andrei Bobrov 2012-9-23
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{:});

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by