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
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
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 个评论
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.
Charles Lee
2020-9-13
My solution is to set all variables as 'char' before merging.
- detectImportOptions() to find the variables with type 'double'
- setvartype() to set the variables as 'char'
- 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];
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!