How do I combine two cell arrays into one cell array? (2024)

404 views (last 30 days)

Show older comments

pengcheng on 13 Oct 2014

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/158461-how-do-i-combine-two-cell-arrays-into-one-cell-array

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/158461-how-do-i-combine-two-cell-arrays-into-one-cell-array

Answered: Josep Llobet on 1 Oct 2021

Accepted Answer: Star Strider

Open in MATLAB Online

>> 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?

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Star Strider on 13 Oct 2014

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/158461-how-do-i-combine-two-cell-arrays-into-one-cell-array#answer_154992

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/158461-how-do-i-combine-two-cell-arrays-into-one-cell-array#answer_154992

Open in MATLAB Online

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 Comments

Show 6 older commentsHide 6 older comments

pengcheng on 13 Oct 2014

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/158461-how-do-i-combine-two-cell-arrays-into-one-cell-array#comment_242725

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/158461-how-do-i-combine-two-cell-arrays-into-one-cell-array#comment_242725

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 on 13 Oct 2014

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/158461-how-do-i-combine-two-cell-arrays-into-one-cell-array#comment_242726

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/158461-how-do-i-combine-two-cell-arrays-into-one-cell-array#comment_242726

You could try: {[Q{:}]}

pengcheng on 13 Oct 2014

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/158461-how-do-i-combine-two-cell-arrays-into-one-cell-array#comment_242728

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/158461-how-do-i-combine-two-cell-arrays-into-one-cell-array#comment_242728

I try to use this,but i doesn't work ,Error using horzcat Dimensions of matrices being concatenated are not consistent.

Star Strider on 13 Oct 2014

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/158461-how-do-i-combine-two-cell-arrays-into-one-cell-array#comment_242729

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/158461-how-do-i-combine-two-cell-arrays-into-one-cell-array#comment_242729

Open in MATLAB Online

Thank you Iain!

Simply doing:

R = Q{:};

also works.

Adam on 13 Oct 2014

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/158461-how-do-i-combine-two-cell-arrays-into-one-cell-array#comment_242730

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/158461-how-do-i-combine-two-cell-arrays-into-one-cell-array#comment_242730

Edited: Adam on 13 Oct 2014

Open in MATLAB Online

R = vertcat(Q{:})

seems to work.

Star Strider on 13 Oct 2014

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/158461-how-do-i-combine-two-cell-arrays-into-one-cell-array#comment_242732

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/158461-how-do-i-combine-two-cell-arrays-into-one-cell-array#comment_242732

Open in MATLAB Online

@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 on 13 Oct 2014

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/158461-how-do-i-combine-two-cell-arrays-into-one-cell-array#comment_242733

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/158461-how-do-i-combine-two-cell-arrays-into-one-cell-array#comment_242733

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 on 13 Oct 2014

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/158461-how-do-i-combine-two-cell-arrays-into-one-cell-array#comment_242740

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/158461-how-do-i-combine-two-cell-arrays-into-one-cell-array#comment_242740

@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!

Sign in to comment.

More Answers (2)

Chenchal on 3 Nov 2017

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/158461-how-do-i-combine-two-cell-arrays-into-one-cell-array#answer_289263

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/158461-how-do-i-combine-two-cell-arrays-into-one-cell-array#answer_289263

cell2mat(Q')

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Josep Llobet on 1 Oct 2021

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/158461-how-do-i-combine-two-cell-arrays-into-one-cell-array#answer_799639

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/158461-how-do-i-combine-two-cell-arrays-into-one-cell-array#answer_799639

Open in MATLAB Online

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

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABLanguage FundamentalsMatrices and ArraysCreating and Concatenating Matrices

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

  • cell array
  • concatenate
  • vertcat

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How do I combine two cell arrays into one cell array? (13)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

How do I combine two cell arrays into one cell array? (2024)
Top Articles
Latest Posts
Article information

Author: Neely Ledner

Last Updated:

Views: 6040

Rating: 4.1 / 5 (62 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Neely Ledner

Birthday: 1998-06-09

Address: 443 Barrows Terrace, New Jodyberg, CO 57462-5329

Phone: +2433516856029

Job: Central Legal Facilitator

Hobby: Backpacking, Jogging, Magic, Driving, Macrame, Embroidery, Foraging

Introduction: My name is Neely Ledner, I am a bright, determined, beautiful, adventurous, adventurous, spotless, calm person who loves writing and wants to share my knowledge and understanding with you.