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

조회 수: 2 (최근 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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by