Joining multiple 1xn cell arrays into mxn cell array

Hello I have a function that returns a containers.Map object with values as 1xn cell arrays. For example
map=containers.Map({'OBJECT', 'COLOR', 'WGT'},{{'obj1','obj2','obj3'},{'red','blue','green'},{1.2,2.54,8.12}});
The values in map are 3 1x3 cells.
>> map.values
ans =
{1x3 cell} {1x3 cell} {1x3 cell}
I would like to join the 3 1x3 cell data into 3x3 cell data like this
>> transpose([{'obj1','obj2','obj3'};{'red','blue','green'};{1.2,2.54,8.12}])
ans =
'obj1' 'red' [1.2000]
'obj2' 'blue' [2.5400]
'obj3' 'green' [8.1200]
How do I do this?
Thanks Bhaskar

 採用された回答

Star Strider
Star Strider 2015 年 9 月 3 日

0 投票

One possibility:
v = map.values;
vc = [v{1}', v{2}', v{3}']
vc =
'red' 'obj1' [1.2000e+000]
'blue' 'obj2' [2.5400e+000]
'green' 'obj3' [8.1200e+000]

4 件のコメント

Bhaskar Edara
Bhaskar Edara 2015 年 9 月 3 日
How do I make this dynamic. The size is not always 3. Can vc be made by a loop or some other construct?
Star Strider
Star Strider 2015 年 9 月 3 日
This works:
vc = [];
for k1 = 1:size(v,2)
vc = [vc, v{k1}'];
end
That assumes they’re all the same length.
Bhaskar Edara
Bhaskar Edara 2015 年 9 月 3 日
I was able to do this using your idea.
vc=cell(3,3);
keys=map.keys;
for i=1:3
vc(:,i)=map(keys{i});
end
Thank you
Star Strider
Star Strider 2015 年 9 月 3 日
My pleasure.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by