获取显示值及其出现次数的数组,制作单独列出值的数组

Take array that shows values with numbers of times they appear, make array that lists values individually

我有一个数组,如下所示:

20  1
20  7
45  3
127 17
102 1
90  1
64  1

等... 其中左栏显示光强度值,右栏显示出现的次数。我想要一个相应的元胞数组来扩展数组并单独列出每个值的出现次数。像这样(忽略左侧单元格列):

[]  20
[]  20
[]  20
[]  20
[]  20
[]  20
[]  20
[]  20
[]  45
[]  45
[]  45

等...谁能告诉我一个聪明的方法来做到这一点?棘手的索引在这里。我有以下笨拙的作品(有点):

% Determine the number of 10-minute intervals for which a light level is
% recorded (sum all the unique light levels and their repeats).  
no_light_levels = sum(repeat_arr(:, 2));
%
% 'geologger_data_all' records time of each light level reading and the
% light level.  
geologger_data_all{no_light_levels, 2} = []; 
geologger_data_all(1, 2) = {repeat_arr(1, 1)};
% 
k_out = 2;                                  % index for out arr.
for k = 2:length(repeat_arr)
    light_level = repeat_arr(k, 1);         % grab the light level
    appears = repeat_arr(k, 2);             % does it repeat?
    if appears == 1
        geologger_data_all(k_out, 2) = {light_level};   % record
        k_out = k_out + 1; 
    elseif appears > 1
        % Record the light level for the number of times it appears.  
        geologger_data_all(k_out:(k_out + appears - 1), 2) = {light_level};
        k_out = k_out + appears;            % advance index for out arr.  
    end
end
%

其中 repeat_arr 看起来像这里显示的第一个格式化数组。在此先感谢您提供任何提示。

你所拥有的本质上是 run-length-encoded data which you can decode with repelem,它将每个元素重复指定的次数

data = [20 1;20 7;45 3;127 17;102 1;90 1;64 1];
out = repelem(data(:,1), data(:,2))

如果您确实需要像您展示的那样将其设为元胞数组,您可以添加以下步骤:

result(:,2) = num2cell(out);

如果您使用的 MATLAB 版本早于 R2015a(在引入 repelem 之前),您可以使用提供的解决方案 here 来获得类似的功能。