Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
different way of "concatenating" cell arrays

Subject: different way of "concatenating" cell arrays

From: Bob S

Date: 6 Jan, 2011 20:15:21

Message: 1 of 6

Say I have a couple of Nx1 cell arrays.
c1 = {'James', 'Carl', 'Sarah'}';
c2 = {'Enright', 'Haynes', 'Latham'}';

How can I combine them to create a cell array that looks like this one?
c = {{'James', 'Enright'}, {'Carl', 'Haynes'}, {'Sarah', 'Latham'}}'

Is there a straightforward way to do this without looping? How about if the two cell arrays are NxM and NxK instead of both Nx1?

FWIW, my motivation is that I have a third Nx1 cell array c3, and c1, c2, c3 are all conceptually part of an Nx3 table, and I want to create a map from c3 elements to {c1 element, c2 element} pairs.

c3 = {'2R175', 'B7398', 'A479GY'}';
m = containers.Map(c3, c);
m('B7398')
ans =
    'Carl' 'Haynes'

Thanks.

Subject: different way of "concatenating" cell arrays

From: Sean de

Date: 6 Jan, 2011 20:50:05

Message: 2 of 6

"Bob S" wrote in message <ig57sp$47s$1@fred.mathworks.com>...
> Say I have a couple of Nx1 cell arrays.
> c1 = {'James', 'Carl', 'Sarah'}';
> c2 = {'Enright', 'Haynes', 'Latham'}';
>
> How can I combine them to create a cell array that looks like this one?
> c = {{'James', 'Enright'}, {'Carl', 'Haynes'}, {'Sarah', 'Latham'}}'


c = cellfun(@(x,y){x,y},c1,c2,'uni',false)

Subject: different way of "concatenating" cell arrays

From: David Young

Date: 6 Jan, 2011 20:51:04

Message: 3 of 6

For the simple case:

c = cellfun(@(a,b) {a, b}, c1, c2, 'UniformOutput', false)

Subject: different way of "concatenating" cell arrays

From: Ade

Date: 6 Jan, 2011 20:55:05

Message: 4 of 6

"Bob S" wrote in message <ig57sp$47s$1@fred.mathworks.com>...
> Say I have a couple of Nx1 cell arrays.
> c1 = {'James', 'Carl', 'Sarah'}';
> c2 = {'Enright', 'Haynes', 'Latham'}';
>
> How can I combine them to create a cell array that looks like this one?
> c = {{'James', 'Enright'}, {'Carl', 'Haynes'}, {'Sarah', 'Latham'}}'
>
> Is there a straightforward way to do this without looping? How about if the two cell arrays are NxM and NxK instead of both Nx1?
>
> FWIW, my motivation is that I have a third Nx1 cell array c3, and c1, c2, c3 are all conceptually part of an Nx3 table, and I want to create a map from c3 elements to {c1 element, c2 element} pairs.
>
> c3 = {'2R175', 'B7398', 'A479GY'}';
> m = containers.Map(c3, c);
> m('B7398')
> ans =
> 'Carl' 'Haynes'
>
> Thanks.

for your example:
 c1 = {'James', 'Carl', 'Sarah'}';
 c2 = {'Enright', 'Haynes', 'Latham'}';

try this:
c3 = cellfun(@(x,y) {x y}, c1,c2, 'Un',0);
answer = c3'

Subject: different way of "concatenating" cell arrays

From: Bruno Luong

Date: 6 Jan, 2011 20:59:07

Message: 5 of 6

"Bob S" wrote in message <ig57sp$47s$1@fred.mathworks.com>...
> Say I have a couple of Nx1 cell arrays.
> c1 = {'James', 'Carl', 'Sarah'}';
> c2 = {'Enright', 'Haynes', 'Latham'}';
>
> How can I combine them to create a cell array that looks like this one?
> c = {{'James', 'Enright'}, {'Carl', 'Haynes'}, {'Sarah', 'Latham'}}'
>
> Is there a straightforward way to do this without looping? How about if the two cell arrays are NxM and NxK instead of both Nx1?

 c = num2cell([c1 c2],2)'

Bruno

Subject: different way of "concatenating" cell arrays

From: Bob S

Date: 6 Jan, 2011 21:19:06

Message: 6 of 6

Thanks, everyone. All three replies gave the same solution, so that's clearly the canonical answer.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us