How to find the index of array that reach to an specific value
조회 수: 2 (최근 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!