13 views (last 30 days)

Hello,

I have a 32 column by one row array. I want to sum across the values in each column until I match a threshold value. The result should provide the column # (which will be between 1:32)

I've tried a few things and havent been able to iron this down.

Here's the data:

A = [0 0 4520 51418 101386 90907 78735 65863 45766 26831 30586 14864 7905 4741 2840 2475 872 290 99 44 15 2 0 0 0 1 0 2 0 0 0 0];

Threshold value is = 0.5*sum(A).

So I want to find the cell where A = 265081.

The result should provide the cell number (or bin if you will) where this value occurs while summing from the first cell of A to the last.

Adam Danz
on 8 Aug 2019

Edited: Adam Danz
on 8 Aug 2019

Compare the cumulative sum against your threshold. The line below returns the first index of A that is greater than or equal to your threshold.

A = [0 0 4520 51418 101386 90907 78735 65863 45766 26831 30586 14864 7905 4741 2840 2475 872 290 99 44 15 2 0 0 0 1 0 2 0 0 0 0];

threshold = 5000;

idx = find(cumsum(A) >= threshold,1);

If you'd rather have the index of the last element that is less than or equal to your threshold,

idx = find(cumsum(A) <= threshold,1,'last');

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/475472-find-the-cell-where-the-sum-equals-a-threshold#comment_733282

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/475472-find-the-cell-where-the-sum-equals-a-threshold#comment_733282

Sign in to comment.