Euphoria66
Euphoria66

Reputation: 13

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.

Upvotes: 1

Views: 109

Answers (1)

Ian Riley
Ian Riley

Reputation: 523

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

Upvotes: 0

Related Questions