【问题标题】:Functions within accumarrayaccumarray 中的函数
【发布时间】:2013-06-24 18:24:01
【问题描述】:

如果我有一列日期和一列数据,我使用此代码来查找每个日期的数据的累积总和,如第三列所示:

orgcumulative=cell2mat(accumarray(day,data,[],@(x){cumsum(x)}));
k=orgcumulative==0;
CVD=orgCVD;
CVD(k)=[];

31,3,3
31,2,5
31,1,6
31,5,11
07,2,2
07,3,4
07,4,9
07,2,11
07,3,14
07,5,19
07,3,22
07,1,23
07,1,24
07,2,26
07,3,29
30,5,5
06,4,4

现在我想将一天内的每个数据点除以当天数据的总和。例如:

31,3,3,3/11
31,2,5,2/11
31,1,6,1/11
31,5,11,5/11    <-- 11 is the sum of data for the 31 date
07,2,2,2/29
07,3,4, %and so on...
07,4,9,
07,2,11,
07,3,14,
07,5,19,
07,3,22,
07,1,23,
07,1,24,
07,2,26,
07,3,29, <-- 29 is the sum of data for the 07 date
30,5,5,1
06,4,4,1

如果我尝试:

fractions=cell2mat(accumarray(day,data,[],@(x){  data/sum(x)  }));

这会将整个第二列除以每个总和。有没有办法限制这种情况,以便每天只对第二列的成员进行除法?

【问题讨论】:

    标签: arrays matlab accumarray


    【解决方案1】:

    使用accumarray 累积每天的总数,然后在访问accumarray 输出时使用day 数组作为索引会不会更容易,如下所示:

    total = accumarray(day, data); % equivalent to accumarray(day, data, [], @sum)
    fractions = data ./ total(day);
    

    【讨论】:

    • 非常感谢。早该想到的!
    猜你喜欢
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 2015-04-12
    • 2013-11-12
    • 1970-01-01
    • 2015-07-25
    • 2013-06-19
    • 1970-01-01
    相关资源
    最近更新 更多