简体   繁体   中英

Calculate mean above certain threshold for each column

My data is in a nxm array named x. 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.

mean(x) obviously does this without specifying a threshold. mean(x>70) performs a truth check and basically returns the percentage of values above the threshold for each column

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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