[title removed]
16 views (last 30 days)
Show older comments
I want to concatenate 1st row of a matrix A to all the rows of another matrix B,then the 2 nd row of matrix A to all the rows of matrix B and so on.Both the matrices are of different order.ex A=[1] and B=[2 3; 3 2],ans C=[1 2 3;1 3 2]. Is it possible in matlab to do this ?
3 Comments
Accepted Answer
Guillaume
on 21 Mar 2016
%demo data
A = [1 2 3;4 5 6;7 8 9]
B = [2 3; 3 2]
%build all combinations of row indices of A and B:
[arows, brows] = ndgrid(1:size(A, 1), 1:size(B, 1));
%iterate over all combinations to build cell array of rows and convert cell array to matrix:
C = cell2mat(arrayfun(@(arow, brow) [A(arow, :), B(brow, :)], arows(:), brows(:), 'UniformOutput', false))
More Answers (0)
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!