How to sum every 29 data and make some condition for them?

조회 수: 1 (최근 30일)
Milad
Milad 2016년 7월 18일
편집: Milad 2016년 7월 22일
I have data for a long time. I would sum them every 30 times continuously (first row to 30th row, then second row to 31th row and 3th row to 32 and ............... to end). finally, I would do some conditions. I attached an excel file to show clearly what is that. I would have it by codes in matlab. is there any way to do that?

채택된 답변

Guillaume
Guillaume 2016년 7월 19일
For your moving sum, use movsum (available since r2016a, before that use a convolution). You can then test all values at once with vectorised comparison:
rainfall = readtable('sample.xlsx', 'Range', 'A1:B43082');
movingsum = movsum(rainfall.Rainfall_mm_, 30, 'EndPoints', 'discard');
%prior to 2016a:
%movingsum = conv(rainfall.Rainfall_mm_, ones(30, 1), 'valid');
condition1 = movingsum > 0.5;
condition2 = condition1(1:end-1) & ~condition1(2:end);
  댓글 수: 1
Milad
Milad 2016년 7월 22일
편집: Milad 2016년 7월 22일
Thanks, is work well. NOTE: the condition 2 should be
condition2 = condition1(2:end) & ~condition1(1:end-1) this codes are helpful for those who wants to work with rainfall events, and not too much familiar in matlab. finally, the events can be extracted handy in excel. in this codes, 30 is interevent time, and 0.5 in minimum depth.

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

추가 답변 (1개)

Alexandra
Alexandra 2016년 7월 19일
편집: Alexandra 2016년 7월 19일
I would try something like this:
for k=1:(numel(A)-30)
B(k) = sum(A(k:k+29));
if B(k) <= 0.5
C(k) = 0;
else
C(k) = 1;
end
if k == 1
D(k) = 0;
else
if C(k) == 1 && C(k-1) == 0
D(k) = 1;
else
D(k) = 0;
end
end
end
Does it work?

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by