Skip to main content
Code Review

Return to Question

edited tags
Link
rolfl
  • 98.1k
  • 17
  • 219
  • 419
edited tags; edited title
Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

Applying correction to a time series with fully vectorized code in Matlab

deleted 2 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

How to efficiently apply Applying correction to a time series with fully vectorized code in Matlab

I face a computation efficiency problem. I have a time series of a non-monotonically drifting variable, that is measurements of objects going through a machine (where the measurement is made) in a production line. The job to do consists of simulating what this time series would yield if there was a correction made to the object each time the measurement drifts above or below a threshold.

To do that I could simply make a for loop, and each time the thresholds are crossed, apply the correction to the rest of the time series. However the time series is very long and the for loop would take too much time to compute. I would like to improve the performance of my code. Does anyone see a way to do this?

Here is a working example using a forfor loop:

ts = [1 2 3 4 3 2 1 0 -1 -2 -3];
threshold = [-3.5 3];
correction = [1 -1];
for i = 1:numel(ts)
 if ts(i) > threshold(2)
 ts(i:end) = ts(i:end) + correction(2);
 elseif ts(i) < threshold(1)
 ts(i:end) = ts(i:end) + correction(1);
 end
end
disp(ts)

The result is and should be the following array:

[1 2 3 3 2 1 0 -1 -2 -3 -3]

How to efficiently apply correction to a time series with fully vectorized code in Matlab

I face a computation efficiency problem. I have a time series of a non-monotonically drifting variable, that is measurements of objects going through a machine (where the measurement is made) in a production line. The job to do consists of simulating what this time series would yield if there was a correction made to the object each time the measurement drifts above or below a threshold.

To do that I could simply make a for loop, and each time the thresholds are crossed, apply the correction to the rest of the time series. However the time series is very long and the for loop would take too much time to compute. I would like to improve the performance of my code. Does anyone see a way to do this?

Here is a working example using a for loop:

ts = [1 2 3 4 3 2 1 0 -1 -2 -3];
threshold = [-3.5 3];
correction = [1 -1];
for i = 1:numel(ts)
 if ts(i) > threshold(2)
 ts(i:end) = ts(i:end) + correction(2);
 elseif ts(i) < threshold(1)
 ts(i:end) = ts(i:end) + correction(1);
 end
end
disp(ts)

The result is and should be the following array:

[1 2 3 3 2 1 0 -1 -2 -3 -3]

Applying correction to a time series with fully vectorized code in Matlab

I face a computation efficiency problem. I have a time series of a non-monotonically drifting variable, that is measurements of objects going through a machine (where the measurement is made) in a production line. The job to do consists of simulating what this time series would yield if there was a correction made to the object each time the measurement drifts above or below a threshold.

To do that I could simply make a for loop, and each time the thresholds are crossed, apply the correction to the rest of the time series. However the time series is very long and the for loop would take too much time to compute. I would like to improve the performance of my code. Does anyone see a way to do this?

Here is a working example using a for loop:

ts = [1 2 3 4 3 2 1 0 -1 -2 -3];
threshold = [-3.5 3];
correction = [1 -1];
for i = 1:numel(ts)
 if ts(i) > threshold(2)
 ts(i:end) = ts(i:end) + correction(2);
 elseif ts(i) < threshold(1)
 ts(i:end) = ts(i:end) + correction(1);
 end
end
disp(ts)

The result is and should be the following array:

[1 2 3 3 2 1 0 -1 -2 -3 -3]
deleted 44 characters in body
Source Link
Simon Forsberg
  • 59.7k
  • 9
  • 157
  • 311
Loading
Post Reopened by Simon Forsberg
Post Closed as "Not suitable for this site" by πάντα ῥεῖ, Phrancis, Dan Oberlam, rolfl
update grammar, formatting
Source Link
Loading
Source Link
Loading
lang-matlab

AltStyle によって変換されたページ (->オリジナル) /