How do I combine two cell arrays into one cell array?

>> Q{1}
ans =
'4400002970000003533'
'4400002970000003533'
'4400002970000003535'
'4400002970000003536'
'4400002970000003533'
'4400002970000003532'
'4400002970000003537'
>> Q{2}
ans =
'4400002890000146180'
'4400002890000146180'
'4400002970000000026'
I want to get a new cell:
'4400002970000003533'
'4400002970000003533'
'4400002970000003535'
'4400002970000003536'
'4400002970000003533'
'4400002970000003532'
'4400002970000003537'
'4400002890000146180'
'4400002890000146180'
'4400002970000000026'
I don't want to use the function cell2mat, because it is too slow for my program. Do you have any good ideas?

 採用された回答

Star Strider
Star Strider 2014 年 10 月 13 日

3 投票

To get the result cell array ‘R’, for instance, vertically concatanate ‘Q{1}’ and ‘Q{2}’:
Q{1} = ['4400002970000003533'
'4400002970000003533'
'4400002970000003535'
'4400002970000003536'
'4400002970000003533'
'4400002970000003532'
'4400002970000003537'];
Q{2} = ['4400002890000146180'
'4400002890000146180'
'4400002970000000026'];
R = {[Q{1}; Q{2}]};
celldisp(R) % Display Result

8 件のコメント

pengcheng
pengcheng 2014 年 10 月 13 日
If i want to combine them from Q{1}to Q{100},i should use a loop ?or you have some good idea,thank you very much
Iain
Iain 2014 年 10 月 13 日
You could try: {[Q{:}]}
pengcheng
pengcheng 2014 年 10 月 13 日
I try to use this,but i doesn't work ,Error using horzcat Dimensions of matrices being concatenated are not consistent.
Star Strider
Star Strider 2014 年 10 月 13 日
Thank you Iain!
Simply doing:
R = Q{:};
also works.
Adam
Adam 2014 年 10 月 13 日
編集済み: Adam 2014 年 10 月 13 日
R = vertcat(Q{:})
seems to work.
Star Strider
Star Strider 2014 年 10 月 13 日
@pengcheng — It is a vertical condatanation, not a horizontal concatanation. The semicolon ; between ‘Q{1}’ and ‘Q{2}’ in my original code is important. It puts ‘Q{1}’ on top of ‘Q{2}’, not beside it, equivalent to:
R = {[Q{1}
Q{2}]};
Adam
Adam 2014 年 10 月 13 日
I assume he is referring to the more generic answer of {[Q{:}]} which does horizontal concatenation by default.
It is one of the many cases where someone asks a question using a neat example then when given a correct answer for that it turns out they actually want to solve the general case that wasn't mentioned in the example!!
Star Strider
Star Strider 2014 年 10 月 13 日
@Adam — The vertcat function is definitely the way to go!
You’re certainly correct on your observation — in more Questions that I care to count, the instance in the question may have little bearing on actual issue!

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

その他の回答 (2 件)

Josep Llobet
Josep Llobet 2021 年 10 月 1 日

0 投票

Maybe this little function could be useful:
function [celltot] = juntar_cells(cell1, cell2)
celltot = cell1;
for ll_cell2 = 1:length(cell2)
celltot{end + 1} = cell2{ll_cell2};
end
end

カテゴリ

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

質問済み:

2014 年 10 月 13 日

回答済み:

2021 年 10 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by