creating for and while loop for an array data

1 view (last 30 days)
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
Jan
Jan on 2 Apr 2022
Edited: Jan on 2 Apr 2022
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
sermet OGUTCU on 2 Apr 2022
Edited: sermet OGUTCU on 2 Apr 2022
assuming array_data=30 x 1, and sliding_window=10; if, if abs(array_data(i)-array_predicted(i)) > 1.0 condition doesn't happen at all, 10 batches of data for for loop should be; array_data(1:10), array_data(11:20), array_data(21:30). For example;
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
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
array_polynomial = polyfit(time_sliding_window,array_data(21:30)',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

Sign in to comment.

Accepted Answer

Voss
Voss on 2 Apr 2022
I believe this has the behavior you are after.
I'm not sure what should happen when you get to the end of array_data, i.e., maybe you want to use a partial window instead of always requiring 10 samples. As written, this code uses a fixed window size of 10 samples.
%%%%%% create random data, 30-by-1
mean_data = 10;
range_data = 3;
array_data = mean_data-range_data/2+range_data*rand(30,1);
%%%%%% end create data
N_data = numel(array_data);
window_size = 10;
%%%%%% plotting setup to show behavior
figure();
ylim(mean_data+range_data*[-1 1]);
hold on
colors = 'brgkmcy';
markers = 'o.xs';
N_colors = numel(colors);
N_markers = numel(markers);
count = 0;
%%%%%% end plotting setup
t = 1:window_size;
while t(end) <= N_data
%%%%%% display stuff to command window
disp('current t:');
disp(t);
%%%%%% end display stuff
array_polynomial = polyfit(t,array_data(t).',2); % 2nd degree polynomial coefficients
array_predicted = polyval(array_polynomial,t); % predicted values from polynomial
%%%%%% plotting stuff
count = count+1;
color = colors(mod(count-1,N_colors)+1);
marker = markers(mod(count-1,N_markers)+1);
plot(t,array_data(t),[marker color],t,array_predicted,color);
temp_data = [array_data(t); array_predicted(:)];
temp_lim = [min(temp_data) max(temp_data)];
line(t([1 end end 1 1]),temp_lim([1 1 2 2 1]), ...
'Color',color,'LineWidth',2,'LineStyle','--');
%%%%%% end plotting stuff
error_row = t(1) - 1 + find(abs(array_data(t).'-array_predicted) > 1, 1);
if isempty(error_row)
t = t+window_size;
else
t = error_row+(1:window_size);
end
%%%%%% display stuff to command window
disp('error_row:');
disp(error_row);
%%%%%% end display stuff
end
current t:
1 2 3 4 5 6 7 8 9 10
error_row:
8
current t:
9 10 11 12 13 14 15 16 17 18
error_row:
current t:
19 20 21 22 23 24 25 26 27 28
error_row:
20
current t:
21 22 23 24 25 26 27 28 29 30
error_row:
25

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!