创建具有相同行的新表的有效方法,但每个表重复不同的次数

cin*_*ico 2 optimization matlab vectorization

假设我创建了这个表和一组值:

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) ;

addedRows = 0 ;
for idxName = 1 : numel(originalTable.names)

    newTable(addedRows +1 : addedRows + nRepeat(idxName) , :) =...
        [repmat(originalTable(idxName ,:), nRepeat(idxName), 1), array2table( (1:1:nRepeat(idxName))', 'VariableNames', {'idxRepetition'}) ] ;

    addedRows = addedRows + nRepeat(idxName);
end
Run Code Online (Sandbox Code Playgroud)

这样可行,但对于大型表来说会变得非常缓慢.

有没有更有效的方法来做到这一点?

Unb*_*ess 8

您可以在索引上使用repelem:

indx = repelem((1:numel(nRepeat)),nRepeat);
idxrep = arrayfun(@(x) 1:1:x,nRepeat,'un',0)'
finalTable = [originalTable(indx, :), table([idxrep{:}]','VariableNames', {'idxRepetition'})];
Run Code Online (Sandbox Code Playgroud)

finalTable:

162×3 table

names    values    idxRepetition
_____    ______    _____________

 'a'      '1'             1     
 'a'      '1'             2     
 'a'      '1'             3     
 'a'      '1'             4     
 'a'      '1'             5     
 'a'      '1'             6     
 'a'      '1'             7     
 'a'      '1'             8     
 'a'      '1'             9     
 'a'      '1'            10     
 'b'      '2'             1     
 'b'      '2'             2     
 'b'      '2'             3  
Run Code Online (Sandbox Code Playgroud)