【问题标题】:How to use percentiles in matlabmatlab中如何使用百分位数
【发布时间】:2017-02-07 00:13:23
【问题描述】:

我想编写一个代码,该代码可以从包含每行两个值的矩阵中计算 10 个百分位数:工作时间数和工资。百分位数应基于工资,但显示工资范围内个人的平均工作时间。

我的矩阵中总共有大约 3000 行,但这是数据的样子(真实数据没有标题,我只是将它们放在这里以使其更具可读性):

Wage/h   Work(h)
12       1572
15       1671
32       1782
78       1254
22       0

我知道prctile 必须是正确的函数才能使用,但我不知道如何使用它。感谢所有帮助。 :-)

【问题讨论】:

    标签: matlab


    【解决方案1】:

    您想使用prctile 计算数据的百分位数。然后,您可以使用bsxfun>= 将每个数据点与每个百分位值进行比较。然后,您可以使用cumsum 为每个数据点提供组索引,然后使用accumarray 计算每个组的平均值。

    % Compute the percentiles
    percentiles = prctile(data(:,1), 0:10:90);
    
    % Determine which percentile each wage is within
    tmp = cumsum(bsxfun(@ge, data(:,1), percentiles), 2);
    group = tmp(:,end);
    
    % Compute the mean hours for each group
    mean_wages = accumarray(group, data(:,2), [], @mean);
    

    【讨论】:

      猜你喜欢
      • 2017-05-07
      • 2014-09-06
      • 2021-11-14
      • 1970-01-01
      • 2018-09-14
      • 2021-08-16
      • 2020-03-19
      • 2017-08-31
      • 2015-09-23
      相关资源
      最近更新 更多