Home > database >  Turning frequency table into raw data in matlab
Turning frequency table into raw data in matlab

Time:09-26

I have a 2-column matrix: Column 1 has events (in the form of integers), column 2 counts how frequently those events occurred. I would like to turn this back into the raw data, effectively the inverse of tabulate(). I.e. turn [0 4; 1 2; 2 2; 3 1; 4 0] into [0 0 0 0 1 1 2 2 3]. Any ideas? Cheers.

CodePudding user response:

If you have

a = [0 4; 1 2; 2 2; 3 1; 4 0];

Then you can simply use the 2nd column as the number of repeats and the first column as the values for repelem

b = repelem( a(:,1), a(:,2) ).';
% >> b = [0     0     0     0     1     1     2     2     3]

With the .' as optional to transpose the result from a column to a row.

  • Related