creating for and while loop for an array data
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Jan
2022-4-2
编辑:Jan
2022-4-2
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)"?
采纳的回答
Voss
2022-4-2
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Directed Graphs 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!