What is the fastest way to add a row at the bottom of a table?

67 次查看(过去 30 天)
Hi all,
In my program, I create an empty table with, lets say, 70 columns. Lets call it 'Collection_Table'.
During a tipical run the program has to add a one row table, 'Row_Table', to the bottom of 'Collection_Table'.
In a tipical run it happens few tens of thousend times (~20000 and more).
It turns that it takes A LOT OF TIME. I tryied the following methods:
1.
Collection_Table(end+1,:) = Row_Table;
2.
Collection_Table = [Collection_Table; Row_Table];
3.
I created a very large 'Cellection_Table' with zero, or empty, entires to eliminate the problem of enlarging the table during run.
Then, I used a counter 'Counter' to track the right entry to the table:
Counter = Counter + 1;
Collection_Talbe(Counter,:) = Row_Table;
In all three methods the time it takes to add 'Row_Table' data was long! Actually it is the major time consuming action in the program.
Is there a way to be more efficient? Am I missing something? Or I have to accept that working with the tables is time consuming?
Thanks,
Alon
  1 个评论
Akira Agata
Akira Agata 2018-12-11
If you know a maximum number of rows, say N, of Collection_Talbe after running your program, how about pre-alocating a N-by-70 empty table ?
To pre-alocate a table, the following example will be some help.

请先登录,再进行评论。

回答(3 个)

Guillaume
Guillaume 2018-12-11
"Or I have to accept that working with the tables is time consuming?"
You have to accept that growing anything (tables, matrices, etc.) one row at a time is time consuming. For efficiency data is stored continuously in memory. Since the memory after your current matrix/table may already be in use, when you add a row, matlab has to:
  • allocate a new block of memory somewhere with enough room for the current data + the new row
  • copy over the current data into that new block
  • add the new row at the end
Do that a few 20,000 times and it adds up to a lot of memory allocations and data copies.
The way to avoid is not to grow things but tell matlab from the start what the final size is going to be by preallocating your matrix/table/whatever, and then filling it one row at a time. If for some reason, there's no way to know beforehand the final size, then preallocate a too large table/whatever, fill it up and trim the extra at the end.
  8 个评论
Alon Rozen
Alon Rozen 2018-12-16
Hi,
I tried but I can't get it write.
The problem is the complicated data I use. If I have to keep it in a cell array, each cell will host a struct who its fields are either number, matrix or logical. The table I get is not a table in which the fiieds turns to colulmns and each stores the data from all structs in all cell. I don't see a way around this.
Alon
Guillaume
Guillaume 2018-12-16
I'm a bit confused. Whichever way you create the table, at some point you must convert these structures into something that can be put into a table. So how did you do it originally?
Perhaps this is that conversion that is actually the bottleneck.
I suggest that you give an example of the data you have. If everything is a structure, perhaps concatenating the lot into a single structure array instead of a cell array is the way to go. You can then convert that structure array into a table with struct2table.

请先登录,再进行评论。


KSSV
KSSV 2018-12-11
T = array2table(rand(3)) ;
T1 = array2table(rand(1,3)) ;
T = [T ; T1]
  1 个评论
Alon Rozen
Alon Rozen 2018-12-11
Thanks KSSV,
The problem is that many of the table's columns contain data which is not numbers. In fact, I have, numbers, matricies, and cells in different columns. So I can't use fast matrix operations to add a new row.
Alon

请先登录,再进行评论。


Charles Lee
Charles Lee 2020-9-13
My solution is to set all variables as 'char' before merging.
  1. detectImportOptions() to find the variables with type 'double'
  2. setvartype() to set the variables as 'char'
  3. Just merge
%% one example→merge Sapfluxnet_info & St_md :
opts = detectImportOptions('Sapflux_St_md.csv','NumHeaderLines',0);
% Just give a hint in case it can't get the variablenames
opts = setvartype(opts,{'st_age','st_basal_area','st_density','st_height'},'char');
%'st_age','st_basal_area','st_density','st_height' → 'double' type variables
Sapfluxnet_info = readtable('Sapflux_St_md.csv',opts);
opts = detectImportOptions('St_md_name.csv','NumHeaderLines',0);
opts = setvartype(opts,{'st_age','st_basal_area','st_density','st_height'},'char');
St_md = readtable('St_md_name.csv',opts);
Sapfluxnet_info = [Sapfluxnet_info;St_md];

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

标签

产品


版本

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by