|
"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'
|