Split a Table at every nth row
조회 수: 32 (최근 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
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!