table block growth slow

7 views (last 30 days)
Alex B
Alex B on 22 Jun 2018
Commented: Peter Perkins on 3 Jul 2018
Hi,
I am trying to fill up a table with mixed data containing cashflows. each mortgage gets multiple lines. Preallocation of a similair table does not seem solve this problem. For preallocation i used repmat to create a table as large as the output containing dummy vars. Because dynamic allocation is complexer to read and gave no performance boost I decided to drop this approach.
The process of dynamically building the table starts out fairly fast but after 35000 iterations, where each adds blocks of about 360 new records, the perfomance decreases very fast. Takes about an hour or more.
enddataset = [dataset;newblock] % dynamic, slows over time
the tictoc slows down. now the weird stuff it that when I use struct it remains fast (11/12 minutes):
myStruct.iterationLabel = newblock. % does not slow over time
As I said using repmat and preallocation does not speed up.
Is it possible to grow a table in a fast manner?

Accepted Answer

Matt J
Matt J on 22 Jun 2018
Edited: Matt J on 22 Jun 2018
If growing the object in struct form is fast enough for you, then a solution would be to build it as a struct first and then transform the final result using struct2table().
  6 Comments
Alex B
Alex B on 25 Jun 2018
Hi Matt,
Your suspicion proved right. I did some reading and indeed for any dynamic growth Matlab copies the whole object. Instead of a table containing strings I coded the strings into numeric and now use a numeric matrix. My preallocation for the table or something else must not have been working since for the numeric matrix it did work. On top of that the computational time went down even further (50%) So I am keeping this.
Solution thanks to your help; use preallocation and use numeric matrix instead of table object with strings.
Thanks Matt!
Peter Perkins
Peter Perkins on 3 Jul 2018
I'm coming late to this thread, but two suggestions:
1) If you know the total size of the final table, you are much better off preallocating the right number of rows with zeros or whatever, and assigning into those rows. In R2018a, there's a new table constructor syntax for preallocation.
2) If that's not possible, you can save each block in a cell array of 360-row tables, and then vertcat(c{:}) all those tables into one at the end. This is usually quite fast.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!