MATLAB Answers

Concatenate cell arrays element-wise

34 views (last 30 days)
Hi, I have two cell arrays: A = {'a','b'}, and C = {'c','d'}.
How can I write a loop to take each element of A and concatenated it with each element of C?
Result = {'ac','ad','bc','bd'}
Any help would be appreciated! Thanks,
Sushma

  0 Comments

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 22 Mar 2015
Edited: Andrei Bobrov on 22 Mar 2015
out = strcat(A(kron(1:2,[1,1])),C(kron([1,1],1:2)));
or
[y,x]=ndgrid(1:2);
out = strcat(A(x(:)),C(y(:)));

  0 Comments

Sign in to comment.

More Answers (0)

Sign in to answer this question.