## Fixing data set with if/else, how do I index/indicate this properly?

L

### L (view profile)

さんによって質問されました 2018 年 10 月 31 日

### L (view profile)

さんによって コメントされました 2018 年 11 月 1 日
dpb

### dpb (view profile)

さんの 回答が採用されました
I have a data set of seconds (plotted image below).
Rather than consistently increasing, they are dropping down and resetting. I am trying to write something that fixes this so the end result will be them spaced as they are now but in a consistently increasing line. I think I want to indicate that each time a value v is lower than the previous (v-1), add the difference between the two to v and keep going. I am new to Matlab and started to write something like this:
for v=1:length(Seconds)
if (Seconds(v+1) <= Seconds(v))
Seconds(v+1) = Sec
onds(v+1)+(Seconds(v)-Seconds(v+1));
else
(Seconds(v+1)) = Seconds(v+1);
end
end
How do I properly assign the new values? Thank you

L

### L (view profile)

2018 年 10 月 31 日
I'm not sure cumsum is what I need to use here. Rather than the cumulative sum, I need to adjust each value such that
if (Seconds(i+1) <= Seconds(i))
Seconds(i+1) = Seconds(i)+(Seconds(i)-Seconds(i+1))
The values are not equally spaced and I do not want to add each of the previous values to every value. Please let me know if there is anything I can clarify.
dpb

### dpb (view profile)

2018 年 10 月 31 日
Yeah, shorten the file to the first one (or at most two) cycles and show us what you think the answer would be to go with it...afaict, what you've written about is what cumsum does.
L

### L (view profile)

2018 年 10 月 31 日
Thanks for the follow up. I've read more about cumsum and if I take, for example, this point marked in the image below, I can understand that I would need to add all the previous values to "stack" it. Because the first line of data is correct, though, I would need to exclude it from cumsum calculations until I need it for the start of the second line set of data, correct? (I want my data points to stay as they are until they drop back down to 0 for the first time)

サインイン to comment.

## 1 件の回答

2018 年 10 月 31 日

### dpb (view profile)

2018 年 11 月 1 日
採用された回答

OK, to increment every element from the previous by its absolute matnitude change is I guess what you're asking to do...
yy=cumsum([y(1);abs(diff(y(:)))]);
In this, then,
sum(diff(yy)<0) --> 0;
every point is the previous plus the change from the previous as if it were still growing rather than being cyclic; or, iow, as if you had rectified the signal.
NB: IF the data are a column you don't need the (:); I used it to ensure knew the orientation of the result from diff to do the concatenation with the first element.
W/ the revision of what the "reset" really is (I had presumed a sampled waveform, not the ramp)...
dy=diff(y); % get the differences
ibrk=find(dy<0); % find the breakpoints
y(ibrk+1)=y(ibrk+1)+abs(dy(ibrk)); % fix up those points to match previous
dy(ibrk)=0; % now eliminate the reset offset
yy=cumsum([y(1); dy]); % build the ramp
This ignores the ramp between the end of one cycle and beginning of next--those two points are same. If you don't want that plateau, you can estimate a slope and introduce an estimated dy at each break instead of setting to zero to cancel the reset.

L

### L (view profile)

2018 年 10 月 31 日
Segment breakpoint at 194/195
dpb

### dpb (view profile)

2018 年 11 月 1 日
Ah! That's a completely different form than I had presumed--that's a periodic reset ramp, not a sampled waveform...the problem w/ too many points on the plot not showing the actual point locations, drew wrong conclusion.