About the usage of for, while and return
Show older comments
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
Bjarke Skogstad Larsen
on 19 May 2020
I think you have a mistake in your code... the expression for stopping the while loops are constant.
What I mean is that you check the same numbers for each iteration of the loop, fx. in the first loop abs(data(1)) > 0.1
Maybe you meant to have it check abs(data(i_1)) > 0.1 or something similar?
Are the calculations for the sections the same just testing the different area within the data array or are the calculations different as well?
At the present we can't see what would change anything inside the loop.
As for
% for all abs(data(xx)) parts, xx equals to array of 1:361:86280;
if xx = data(1:361:86280), then xx has 240 elements ==>
>> 86280/361
ans =
239
>>
plus 1 for the start/end points over 239 intervals.
One simplification would be could write
(abs(data(723)) > 0.10 || abs(data(724)) > 0.10 || abs(data(725) > 0.10
as
IDX=723;
TOL=0.10;
any(abs(data(IDX:IDX+2))>TOL)
sermet OGUTCU
on 19 May 2020
Edited: sermet OGUTCU
on 19 May 2020
sermet OGUTCU
on 19 May 2020
dpb
on 19 May 2020
In that case, just write an indexing vector and group the entire lot---
Accepted Answer
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices 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!