Distributing a cell array into another
Show older comments
If I have 2 cell arrays of chars, is it possible to distribute them such that they are combined and concatenated?
A = {'Category1', 'Category2'};
B = {'Mon', 'Tues', 'Wed', 'Thurs' 'Fri', 'Sat', 'Sun'};
Result = {'Category1 Mon', 'Category1 Tues', ..., 'Category1 Sun', 'Category2 Mon', 'Category2 Tues', ..., 'Category2 Sun'};
Accepted Answer
More Answers (2)
the cyclist
on 18 May 2020
Edited: the cyclist
on 18 May 2020
Here is a straightforward way:
A = {'Category1', 'Category2'};
B = {'Mon', 'Tues', 'Wed', 'Thurs' 'Fri', 'Sat', 'Sun'};
nA = numel(A);
nB = numel(B);
Result = cell(1,nA*nB);
nc = 0;
for ia = 1:nA
for ib = 1:nB
nc = nc + 1;
Result{nc} = [A{ia},' ',B{ib}];
end
end
Fangjun Jiang
on 18 May 2020
0 votes
strcat(repmat(A,size(B')),repmat({' '},[length(B),length(A)]),repmat(B',size(A)))
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!