Applying correction to a time series with fully vectorized code in Matlab
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]