Cummulative sum between NaN values

1 view (last 30 days)
I have the fllowing Rainfall Data
NaN
0.250000000000000
0
0
0.250000000000000
0
0
0
0.250000000000000
0
0.250000000000000
0
0
0.250000000000000
NaN
0.250000000000000
0
0
0
0.250000000000000
0.250000000000000
NaN
0.250000000000000
0.760000000000000
I would like to find the cummulative Sum between the NaN. It should be:
NaN
0.250000000000000
0.250000000000000
0.250000000000000
0.500000000000000
0.500000000000000
0.500000000000000
0.500000000000000
0.750000000000000
0.750000000000000
1.000000000000000
1.000000000000000
1.000000000000000
1.250000000000000
NaN
0.250000000000000
0.250000000000000
0.250000000000000
0.250000000000000
0.500000000000000
0.500000000000000
NaN
0.250000000000000
0.910000000000000

Accepted Answer

Chunru
Chunru on 6 Apr 2022
Edited: Chunru on 6 Apr 2022
The result you gave above seems not correct.
x=[ NaN
0.250000000000000
0
0
0.250000000000000
0
0
0
0.250000000000000
0
0.250000000000000
0
0
0.250000000000000
NaN
0.250000000000000
0
0
0
0.250000000000000
0.250000000000000
NaN
0.250000000000000
0.760000000000000];
idx = find(isnan(x));
y = x;
idx = [0; idx; length(x)+1];
for i=1:length(idx)-1
y(idx(i)+1:idx(i+1)-1) = cumsum(x(idx(i)+1:idx(i+1)-1));
end
disp(y)
NaN 0.2500 0.2500 0.2500 0.5000 0.5000 0.5000 0.5000 0.7500 0.7500 1.0000 1.0000 1.0000 1.2500 NaN 0.2500 0.2500 0.2500 0.2500 0.5000 0.7500 NaN 0.2500 1.0100

More Answers (0)

Community Treasure Hunt

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

Start Hunting!