简体   繁体   English

根据数组位置和计数器计算设定值的平均值

[英]Calculate mean of set values based on array position and counter

I have two vectors in MATLAB as follows: 我在MATLAB中有两个向量,如下所示:

  Crossing Time = [11 14 16 18 29 18 53 22 18 11 19 10 15 15 13 18];

          Count = [0  0  2  3  0  0  4  3  6  2  5  0  3  5  2  7];

I want to calculate mean of crossing time based on the count. 我想根据计数计算穿越时间的平均值。 For example: If count is zero, corresponding crossing time is just the value from the crossing time vector ie for first zero in count vector, crossing time is 11 and for second zero the crossing time is 14. This is straightforward. 例如:如果count为零,则对应的穿越时间就是穿越时间向量的值,即对于计数向量中的第一个零,穿越时间为11,对于第二个零,穿越时间为14。这很简单。 However, if the count is non-zero, such as 2 (third column in count vector), I want the mean of 2nd and 3rd columns of crossing time vector beside 2 (ie mean of 14 and 16 beside 2). 但是,如果计数不为零,例如2(计数向量中的第三列),则我希望交叉时间向量的第二列和第三列的平均值在2旁边(即2旁边的14和16的平均值)。 If the count is 4 (7th column in count vector) then I want the mean of 4th to 7th columns of the crossing time vector. 如果计数为4(计数向量中的第7列),那么我想要交叉时间向量的第4列至第7列的平均值。

In doing that I would have the following mean vector: 这样做,我将得到以下均值向量:

          Mean = [11 14 15 16 29 18 29.5 31 26.33 14.5 24.6 10 14.67 14 14 14.43] 

How can I do this in a loop so that I can repeat this for my huge vectors with 90,000 records. 如何循环执行此操作,以便可以对具有90,000条记录的庞大向量重复此操作。 It should be straightforward for any programmer but unfortunately I have very limited programming knowledge. 对于任何程序员来说,它应该都很简单,但是不幸的是,我的编程知识非常有限。

Thanks for your help. 谢谢你的帮助。

@thewaywewalk gave a nice answer. @thewaywewalk给出了一个很好的答案。 Just for the record, here is a modified version which precomputes the sums to run faster: 仅作记录,这是一个修改后的版本,可预先计算总和以使其运行更快:

cumulative = [0 cumsum(CrossingTime)];
meanVal = arrayfun(@(x) (cumulative(x + 1) - cumulative(x - max(Count(x), 1) + 1)) / max(Count(x), 1), 1:numel(Count));
clear cumulative;

Running times with a randomized 90.000 array (as mentioned on the question): 90.000个随机数组的运行时间(如问题所提及):

  • thewaywewalk' solution (using loops): 10.163953 seconds thewaywewalk'解决方案(使用循环): 10.163953秒
  • thewaywewalk' solution (using arrayfun): 11.339777 seconds thewaywewalk'解决方案(使用arrayfun): 11.339777秒
  • my solution (precomputing + arrayfun): 0.371947 seconds 我的解决方案(预计算+ arrayfun): 0.371947秒

NOTE: The array was created using this: 注意:数组是使用以下方法创建的:

N = 90000;
CrossingTime = randi(10000, 1, N);
Count = min(0:N-1, randi(N, 1, N) - 1);

Assuming there is no Count = 1 but Count = 0 behaves like Count = 1 , 假设没有Count = 1Count = 0行为类似于Count = 1

Count(~Count) = 1;
cs = [0 cumsum(CrossingTime)];    
meanVal = arrayfun(@(x,y) (cs(x+1) - cs(1 + x-y))/y, 1:numel(Count), Count)

The loop could be as follows: 循环可能如下所示:

Count(~Count) = 1
meanVal = zeros(1,numel(CrossingTime));
for ii = 1:numel(CrossingTime)
    meanVal(ii) = mean( CrossingTime(ii:-1:ii-Count(ii)+1));
end

Alternatively use arrayfun , which is basically the same: 或者使用arrayfun ,这基本上是相同的:

Count(~Count) = 1
meanVal = arrayfun(@(x) mean( CrossingTime(x:-1:x-Count(x)+1)), 1:numel(Count))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM