필터 지우기
필터 지우기

Start Cumsum from table of values that correspond with given indeces

조회 수: 1 (최근 30일)
Jonathan Cheong
Jonathan Cheong 2020년 12월 16일
댓글: Jonathan Cheong 2020년 12월 28일
Hello, I have a timetable of values called ddtable with column 'RAIN' and an array of index called startdd.
The objective:
1) If index of ddtable matches with startdd, start cumsum for subsequent RAIN values until it exceeds 3.5 then stop.
2) Append all indices that were involved in the cumsum into a new table
How do I go about this because traditional while loop does not work as well and no discussion have addressed this type of question.
for ii = 1:length(rain)
% If rain index matches with startdd, start cumulation
if idx(rain) == startdd
% The cumulation will end once it exceeds 3.5mm
while cumsum(rain(startdd)) < 3.5
% Append all index values that were involved in the cumsum
end
end
end
Many thanks!
  댓글 수: 2
Eric Sofen
Eric Sofen 2020년 12월 18일
The data in your MAT files doesn't match up with your example code, so I'm not 100% sure what you're trying to do, but see if this does the trick.
load('ddtable.mat')
load('startdd.mat')
result =ddtable([],:);
inds = [];
for ii = startdd % Iterate over start indices
cs = cumsum(ddtable.RAIN(ii:end)); %do cumsum over most of the rain data - we'll end up throwing away anything beyond 3.5 mm
inds3_5 = ii-1 + find(cs <= 3.5); % Find where data is below the 3.5mm threshold; adjust based on the start index.
result = [result; ddtable(inds3_5,:)]; % append data
inds = [inds; inds3_5]; % append indices
end
Jonathan Cheong
Jonathan Cheong 2020년 12월 28일
Wow! Thank you very much Eric this is exactly what I was looking for.
However, after studying the results another condition that I thought I had eliminated beforehand emerged.
Condition: The cumsum cannot be less than 4 days.
So for example, there are 3 cumsum instances:
A) 15
B) 60, 61, 62, 63...
C) 70, 72
How do I remove A and C but not B from the result?
Ps: If I have to start a new question let me know.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

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