How to find the index of array that reach to an specific value
3 ビュー (過去 30 日間)
古いコメントを表示
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
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
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
2022 年 12 月 17 日
How about this:
v = randi(9, 1, 20)
c = cumsum(v)
thresholds = 10 : 10 : 10*length(c)
for k = 1 : length(c)
t = find(c >= thresholds(k), 1, 'first');
if ~isempty(t)
indexes(k) = t;
end
end
% Show indexes
indexes
% Show cumulative sums at those indexes.
cumValues = c(indexes)
If it's not what you want, explain in detail why it's not.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!