Concatenation with array of different dimensions
2 views (last 30 days)
Show older comments
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]
0 Comments
Answers (2)
Wayne King
on 23 Sep 2012
Edited: Wayne King
on 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);
0 Comments
Andrei Bobrov
on 23 Sep 2012
Edited: Andrei Bobrov
on 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{:});
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!