creating for and while loop for an array data
Show older comments
array_data=30 x 1 % double
sliding_window=10;
time_sliding_window = 1:1:10; % constant for time_sliding_window
array_polynomial = polyfit(time_sliding_window,array_data(1:10)',2); %2nd degree polynomial coefficients
array_predicted=polyval(array_polynomial,time_10); %predicted values from polynomial
for i=1:sliding_window
if abs(array_data(i)-array_predicted(i)) > 1.0
error_row_1(i)=i;
if exist('error_row_1')==1
break
end
end
end
% assume error_row_1=5
if exist('error_row_1')==1
% reset parameters
array_polynomial = polyfit(time_sliding_window,array_data(6:15)',2);
array_predicted=polyval(array_polynomial,time_sliding_window); %predicted values from polynomial
% run again the algorithm
for i=1:error_row_1+1:error_row_1+sliding_window % starts after the error_row to another 10 data
if abs(array_data(i)-array_predicted(i)) > 1.0
error_row_2(i)=i;
if exist('error_row_2')==1
error_row=[error_row_1;error_row_2];
break
end
end
end
end
if exist('error_row_1')==0
% reset parameters
array_polynomial = polyfit(time_sliding_window,array_data(11:20)',2);
array_predicted=polyval(array_polynomial,time_10); %predicted values from polynomial
for i=1:sliding_window
if abs(array_data(i)-array_predicted(i)) > 1.0
error_row_2(i)=i;
if exist('error_row_2')==1
break
end
end
end
end
.......
In these above codes,
if abs(array_data(i)-array_predicted(i)) > 1.0
this code needs to run 10 by 10 within the size of array_data (384). If this if condition happens, error_row should be created, then this if condition needs to be updated started from error_row+1 to error_row+10. If this if condition doesn't happen, array_data needs to be increased by 10 (from array_data(1:10) to array_data(11:20) until it reaches array_data(21:30).
How can I write a compact code for the above computations?
2 Comments
This piece of code is meaningless:
error_row_1(i)=i;
if exist('error_row_1')==1
break
end
Reduce this to:
error_row_1(i)=i;
break
Or even better: Replace
for i=1:sliding_window
if abs(array_data(i)-array_predicted(i)) > 1.0
error_row_1(i)=i;
if exist('error_row_1')==1
break
end
end
end
by
error_row_1 = find(abs(array_data - array_predicted) > 1.0)
The other exist() calls are not useful also.
What does this mean: "this code needs to run 10 by 10 within the size of array_data (384)"?
sermet OGUTCU
on 2 Apr 2022
Edited: sermet OGUTCU
on 2 Apr 2022
Accepted Answer
More Answers (0)
Categories
Find more on Polynomials in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!