MATLAB Answers

inefficient loop to vertically concatenate tables

12 views (last 30 days)
I have a 300000 by 2 table, called output, that looks like in the screenshot below. Each entry in the 2nd column as inside it a 3-column table with different numbers of rows. I woud like to havee one single table with 3-columns, that is, vertically merge all those little 3-column tables into one.
So far, I was doing:
tableBig = array2table([]);
for ii = 1 : size(output,1)
temp = output{ii,2};
temp = temp{:};
tableBig = [tableBig; temp];
end
The problem is that this loop is a inefficient time wise. The first iterations take in the order of 0.0025 seconds which is not too too bad. But as tableBig gets bigger, iterations seem to take longer and longer, so that projected time based on tic/toc is more than 24 hours.
Is there a way to do this without a loop or to speed up the loop?
Thanks
Screenshot 2019-08-08 at 11.15.26.png

  0 Comments

Sign in to comment.

Accepted Answer

Jos (10584)
Jos (10584) on 8 Aug 2019
You can apply comma-separated list expansion to tables too, so this one-liner should work.
tableBig = cat(1, output{:,2})

  5 Comments

Show 2 older comments
Rómulo Alves
Rómulo Alves on 8 Aug 2019
Thanks to both. However, when I do that, I get the following. I would like to have as an output all the contents of the small tables, i.e., a very long table with 3 columns.
I'd like to have the contents of the 6 by 3 matrix below the contents of the 12 by 3 matrix and so on.
Is that feasible?
Screenshot 2019-08-08 at 12.12.29.png
Guillaume
Guillaume on 8 Aug 2019
Oh, yes of course, the tables are stored inside a cell array in the enclosing table. You'll need another cat|vertcat:
c = vertcat(output{:, 2}); %or cat(1, output{:, 2}); %it's the same
tableBig = vertcat(c{:}); %or cat(1, c{:}); %it's the same
I don't think you can do it efficiently in just one step.
Rómulo Alves
Rómulo Alves on 8 Aug 2019
thanks a lot! that works.
my code is still not working properly, so I have added the full description of my problem and my goal here:
If you have time, please read it! thanks!

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Tags