我有一个包含百分比值的矩阵,其中每一行代表一个单独的观察值。我需要计算这些值对应于相同下标的累积乘积。我尝试使用该accumarray
函数,只要我使用列向量作为值(而不是矩阵),它就可以正常工作并且符合预期。我想知道在不遍历值矩阵的各个列的情况下解决问题的最佳方法是什么?
这是我的示例代码:
subs = [1;1;1;2;2;2;2;2;3;3;4;4;4];
vals1 = [0.1;0.05;0.2;0.02;0.09;0.3;0.01;0.21;0.12;0.06;0.08;0.12;0.05];
% This is working as expected
result1 = accumarray(subs,vals1, [], @(x) prod(1+x) -1)
vals2 = [vals1,vals1];
% This is not working as the second input parameter of accumarray
% apperently must be a vector (rather than a matrix)
result2 = accumarray(subs, vals2, [], @(x) prod(1+x) -1)