How to find the index of array that reach to an specific value
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Torsten
am 17 Dez. 2022
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
am 17 Dez. 2022
@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?
Antworten (1)
Image Analyst
am 17 Dez. 2022
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 Kommentare
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!