MATLAB Answers

L
0

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

L
さんによって質問されました 2018 年 10 月 31 日
最新アクティビティ L
さんによって コメントされました 2018 年 11 月 1 日
dpb
さんの 回答が採用されました
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

  4 件のコメント

L
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
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
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 件の回答

dpb
回答者: dpb
2018 年 10 月 31 日
編集済み: dpb
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.
ADDENDUM
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.

  8 件のコメント

L
2018 年 10 月 31 日
Segment breakpoint at 194/195
dpb
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.
See ADDENDUM above...
L
2018 年 11 月 1 日
Thanks, this was great. I really appreciate the help!

サインイン to comment.



Translated by