假设我创建了这个表和一组值:
names = {'a'; 'b'; 'c'; 'd'} ; values = {'1'; '2'; '3'; '4'};
originalTable = table(names, values, 'VariableNames', {'names', 'values'});
nRepeat = [10, 50, 100, 2] ;
Run Code Online (Sandbox Code Playgroud)
我想创建一个新表,它将包含每行重复nRepeat对应索引的次数,即我将第一行或原始表重复10次,然后原始表的第二行重复50次,等等...此外,我想用重复索引向新表添加一列.
我做了什么:
% Initialize newTable to allocate memory space
totalRepetitions = sum(nRepeat) ;
% Repeated first row of the original table the same number of times as the totalRepetitions that will happen, also adding the new column with the index of repetition
newTable = repmat([originalTable(1,:), array2table(1, 'VariableNames', {'idxRepetition'})], totalRepetitions , 1) ; …Run Code Online (Sandbox Code Playgroud)