MATLAB Answers

About the usage of for, while and return

1 view (last 30 days)
sermet OGUTCU
sermet OGUTCU on 19 May 2020
Edited: dpb on 19 May 2020
I need to perform while loop many times as follows;
data=xlsread('data.xlsx','B1:B86640');
% first part
i_1=1;
while (abs(data(1)) > 0.10 || abs(data(2)) > 0.10 || abs(data(3)) > 0.10)
i_1=i_1+1;
if i_1==361;
errordlg('iteration cannot be converged', 'Error!', 'modal')
return
end
end
% second part
i_2=1;
while (abs(data(362)) > 0.10 || abs(data(363)) > 0.10 || abs(data(364)) > 0.10)
i_2=i_2+1;
if i_2==361;
errordlg('iteration cannot be converged', 'Error!', 'modal')
return
end
end
%third part
i_3=1;
while (abs(data(723)) > 0.10 || abs(data(724)) > 0.10 || abs(data(725)) > 0.10)
i_3=i_3+1;
if i_3==361;
errordlg('iteration cannot be converged', 'Error!', 'modal')
return
end
end
After these excecutions, I need to store i_1, i_2 and i_3 arrays.
% for all abs(data(xx)) parts, xx equals to array of 1:361:86280;
How can I execute the above steps avoiding the repeated parts using a single part of different codes?

  5 Comments

Show 2 older comments
sermet OGUTCU
sermet OGUTCU on 19 May 2020
Larsen, I corrected the parentheses error. I executed the codes and It works what it supposed to be. Of course, I need to execute each part seperately if return condition happens.
sermet OGUTCU
sermet OGUTCU on 19 May 2020
@dpv, yes, the calculations for the sections the same just testing the different area within the data array.
dpb
dpb on 19 May 2020
In that case, just write an indexing vector and group the entire lot---

Sign in to comment.

Accepted Answer

dpb
dpb on 19 May 2020
Edited: dpb on 19 May 2020
With the clarification that operations are the same, then
data=xlsread('data.xlsx','B1:B86640');
% preliminaries -- define stopping criteria data points
N=2; % number consecutive points from
IDX=[1:361:723]; % starting points
TOL=0.10; % tolerance
idxvec=cell2mat(arrayfun(@(ix) colon(ix,ix+N),IDX,'UniformOutput',false)); % expand indices to vector
MAXITER = 361;
iter=0;
while any(abs(data(idxvec)>TOL)
iter=iter+1;
if iter>=MAXITER
errordlg('iteration cannot be converged', 'Error!', 'modal')
return
end
data=yourfunctionthatchangesdatagoeshere(iter,data); % do whatever it is need to do...
end

  0 Comments

Sign in to comment.

More Answers (0)

Tags


Translated by