concatenate two cells of different dimention into a single array

1 回表示 (過去 30 日間)
navan
navan 2016 年 6 月 7 日
コメント済み: Star Strider 2016 年 6 月 7 日
I have three cells
so for example
Cell1 is 1x3 that is {1;1;1}
Cell2 is 3x2 that is {2,2;3,3;4,4}
Cell3 is 1x2 that is {1;1}
I want to concatenate all these cells into a single array so that i can see the results as
1 2 2 1
1 3 3 1
1 4 4

採用された回答

Star Strider
Star Strider 2016 年 6 月 7 日
This works:
Cell1 = {1;1;1};
Cell2 = {2,2;3,3;4,4};
Cell3 = {1;1};
Cell3{3} = []; % Add Empty Element To End Of ‘Cell3’
Result = cat(2, Cell1, Cell2, Cell3)
Result =
[1.0000e+000] [2.0000e+000] [2.0000e+000] [1.0000e+000]
[1.0000e+000] [3.0000e+000] [3.0000e+000] [1.0000e+000]
[1.0000e+000] [4.0000e+000] [4.0000e+000] []
  2 件のコメント
navan
navan 2016 年 6 月 7 日
Dear Star Strider, Thanks a lot for your help. Is there any other way than not including empty element. I have many cells like this which is varying in their rows and columns size. So adding blank element will be quite difficult.
Star Strider
Star Strider 2016 年 6 月 7 日
My pleasure.
I am not aware of any way to do what you want without adding an empty element. All the rows and columns have to be equal. You can make it more general with something like this:
Cell1 = {1;1;1};
Cell2 = {2,2;3,3;4,4};
Cell3 = {1;1};
idx = [size(Cell3,1):size(Cell1,1)-1]+1;
Cell3{idx} = []; % Add Empty Element To End Of ‘Cell3’
Result = cat(2, Cell1, Cell2, Cell3)
I do not know how robust that will be with other matrices, but it works here. It gives you a way of having the code calculate the number of empty elements to insert.

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

その他の回答 (0 件)

カテゴリ

Help Center および 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