totable() duplicate rows in matlab

To duplicate rows in a MATLAB table, you can use the repmat() function. The repmat() function repeats the elements of an array. To use repmat() with a table, you can convert the table to a cell array, use repmat() to duplicate the cell array rows, and then convert the duplicated cell array back to a table.

Here's an example code that shows how to duplicate rows in a MATLAB table:

main.m
% Create a sample table with three rows
t = table({'John';'Mary';'Bob'}, [25;32;47], [true;false;true], 'VariableNames', {'Name', 'Age', 'HasChildren'})

% Convert the table to a cell array
c = table2cell(t)

% Duplicate the rows of the cell array
c_dup = repmat(c, 2, 1)

% Convert the duplicated cell array back to a table
t_dup = cell2table(c_dup, 'VariableNames', {'Name', 'Age', 'HasChildren'})

401 chars
13 lines

In this example, the repmat() function is used to duplicate the rows of the c cell array using the syntax repmat(c, 2, 1), which duplicates the rows of c two times. The resulting c_dup cell array is then converted back to a table using cell2table() to create t_dup. The resulting table will have 6 rows, with each original row duplicated 2 times.

related categories

gistlibby LogSnag