简体   繁体   English

计算每列的特定阈值以上的平均值

[英]Calculate mean above certain threshold for each column

My data is in a nxm array named x. 我的数据位于名为x的nxm数组中。 What I want to do is calculate the average of the values in each of the columns above a certain threshold. 我想要做的是计算高于某个阈值的每列中的值的平均值。 So the output should be a 1xm vector. 所以输出应该是1xm向量。

mean(x) obviously does this without specifying a threshold. mean(x)显然没有指定阈值。 mean(x>70) performs a truth check and basically returns the percentage of values above the threshold for each column mean(x>70)执行真值检查,基本上返回每列高于阈值的值的百分比

You could define a new variable as such 您可以这样定义一个新变量

y = x > 70

and then 接着

mean(x(y))

but this returns the average over all the columns of x. 但是这会返回x的所有列的平均值。

There's a very cumbersome way of doing it by having a line of code for every column. 通过为每列提供一行代码,这是一种非常麻烦的方法。

mean(x(y(1:end,1)))

And so on, but this is obviously ugly. 等等,但这显然是丑陋的。

I feel like I'm missing something simple here. 我觉得我在这里缺少一些简单的东西。 Hopefully someone will be able to help out. 希望有人能够提供帮助。

You've forgotten that you can multiply the mask through element-wise 你已经忘记了你可以通过元素来增加掩码

threshold = []; %define threshold here; it can be a 1xm vector or a scalar.
output = sum(x.*(x>threshold))./sum(x>threshold);

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

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