How to duplicate the number of rows in a table based on frequency

29 Ansichten (letzte 30 Tage)
Blue
Blue am 30 Jun. 2020
Kommentiert: Blue am 1 Jul. 2020
Hi,
I have a frequency table like this where N is the number of occurences:
id = [1004, 1004, 1004, 1004]';
length = [23, 45, 56, 78]';
N = [1, 4, 2, 1]';
T = table(id, length, N)
Is it possible to multiply each row by the number of occurences to get a table like this ?
id2 = [1004, 1004, 1004, 1004, 1004, 1004, 1004, 1004]';
length2 = [23, 45, 45, 45, 45, 56, 56, 78]';
N2 = [1, 1, 1, 1, 1, 1, 1, 1]';
T2 = table(id2, length2, N2)
Thank you,

Akzeptierte Antwort

Adam Danz
Adam Danz am 30 Jun. 2020
T2 = T(repelem(1:height(T),T.N),:);
T2.N = ones(size(T2.N));

Weitere Antworten (0)

Kategorien

Mehr zu Debugging and Analysis finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by