Split a Table at every nth row
41 次查看(过去 30 天)
显示 更早的评论
I have a table that is approximately 96,000 lines long, i need to split it into tables that are approximately 28,000 lines long, how would i go about doing this?
1 个评论
Stephen23
2023-10-17
Note that in general splitting up data makes it harder to work with.
MATLAB has plenty of functions and tools that identify and process groups of data within tables:
回答(2 个)
the cyclist
2023-10-17
Here is one way:
% Make up a table
var1 = rand(96000,1);
var2 = rand(96000,1);
tbl = table(var1,var2);
% Define the smaller table size
smallSize = 28000;
% Break it up, storing each smaller table in a cell array
numberTables = ceil(height(tbl)/smallSize);
smallTables = cell(numberTables,1);
for nt = 1:numberTables-1
indexToThisChunk = ((nt-1)*smallSize+1):(nt*smallSize);
smallTables{nt} = tbl(indexToThisChunk,:);
end
smallTables{numberTables} = tbl((numberTables-1)*smallSize+1:end,:);
0 个评论
Voss
2023-10-17
T = array2table(rand(96014,10)) % approximately 96000 rows in the table
One approach: Split T into tables that are exactly 28000 rows long, plus another smaller table for the leftover rows at the end:
n_rows = 28000;
n_tables = ceil(size(T,1)/n_rows);
C = cell(n_tables,1);
for ii = 1:n_tables
C{ii} = T((ii-1)*n_rows+1:min(ii*n_rows,end),:);
end
disp(C);
Another approach: Split T into (almost) equally-sized tables, each with as close to 28000 rows as you can get:
n_rows_target = 28000;
n_rows_T = size(T,1);
n_tables_min = floor(n_rows_T/n_rows_target);
n_tables_max = ceil(n_rows_T/n_rows_target);
n_rows_max = n_rows_T/n_tables_min;
n_rows_min = n_rows_T/n_tables_max;
if n_rows_max - n_rows_target > n_rows_target - n_rows_min
n_rows = floor(n_rows_min);
n_tables = n_tables_max;
else
n_rows = floor(n_rows_max);
n_tables = n_tables_min;
end
n_rows_extra = n_rows_T - n_rows*n_tables;
n_rows = n_rows * ones(1,n_tables);
n_rows(1:n_rows_extra) = n_rows(1:n_rows_extra) + 1;
start_idx = 1 + cumsum([0 n_rows]);
C = cell(n_tables,1);
for ii = 1:n_tables
C{ii} = T(start_idx(ii):start_idx(ii+1)-1,:);
end
disp(C);
2 个评论
Voss
2023-10-19
In both approaches, the tables are stored in the cell array C.
You can write each table to a different sheet of a xls file like this:
output_file = 'tables.xls';
for ii = 1:numel(C)
writetable(C{ii},output_file,'Sheet',ii);
end
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!