How to find the index of array that reach to an specific value

6 ビュー (過去 30 日間)
arash rad
arash rad 2022 年 12 月 17 日
回答済み: Image Analyst 2022 年 12 月 17 日
Hi everyone I have an array like this :
Columns 1 through 9
0 0 2 4 4 5 5 5 5
Columns 10 through 18
5 4 5 5 2 3 6 5 5
Columns 19 through 27
4 4 2 10 3 6 4 5 5
Column 28
2
and I want to calculate when in this array sum of how many of this element reaches to 10 and we can see in index 5 it reaches 10 then A = 5*T
I use cumsum for it and it gives me answer in first step
but again I don't know how to calculate the continue I mean that I want to calculate when again after index 5 the sum of element reach to 10 .
can anyone help me with it
  2 件のコメント
Torsten
Torsten 2022 年 12 月 17 日
I use cumsum for it and it gives me answer in first step
but again I don't know how to calculate the continue I mean that I want to calculate when again after index 5 the sum of element reach to 10
Then look up when cumsum reaches 20.
the cyclist
the cyclist 2022 年 12 月 17 日
@arash rad, suppose when you reach a value of at least 10, you actually get the value 14 (not exactly 10). Do you want to include that "extra" 4 as you sum toward 20, or start over from 0 for the next sum?

サインインしてコメントする。

回答 (1 件)

Image Analyst
Image Analyst 2022 年 12 月 17 日
How about this:
v = randi(9, 1, 20)
v = 1×20
8 5 5 4 2 3 7 5 5 5 6 2 6 6 3 7 1 8 3 9
c = cumsum(v)
c = 1×20
8 13 18 22 24 27 34 39 44 49 55 57 63 69 72 79 80 88 91 100
thresholds = 10 : 10 : 10*length(c)
thresholds = 1×20
10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200
for k = 1 : length(c)
t = find(c >= thresholds(k), 1, 'first');
if ~isempty(t)
indexes(k) = t;
end
end
% Show indexes
indexes
indexes = 1×10
2 4 7 9 11 13 15 17 19 20
% Show cumulative sums at those indexes.
cumValues = c(indexes)
cumValues = 1×10
13 22 34 44 55 63 72 80 91 100
If it's not what you want, explain in detail why it's not.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by