MATLAB Answers

Never ending while-loop

9 ビュー (過去 30 日間)
Nicle Davidson
Nicle Davidson 2021 年 9 月 11 日
コメント済み: Nicle Davidson 2021 年 9 月 12 日
I have a while-loop that I can not get out of.
What I get out of my test is that:
we are in the while-loop
I turn the wheels one step-Pos == 1
we are in the while-loop
I turn the wheels one step-Pos == 1
we are in the while-loop
I turn the wheels one step-Pos == 1
we are in the while-loop
I turn the wheels one step-Pos == 1
we are in the while-loop
I turn the wheels one step-Pos == 1
etc.
It shows that my code goes into the while-loop, where it says 'we are in the while-loop', and then it goes into the if statement, checks to which position we are, then it do the strcmp, and if true goes into it, if not goes to else, it should however make the Pos variable 0 when has been to Pos 1 and vice versa.
howerver it goes into the if for Pos 1 and strcmp somehow is false and it goes to else, and it does the same again.
could I stop this while loop and do what is the goal with it?
notFound is used to stop this while looping, but maybe I have put it wrong place?
*I need to go first to Pos 1 test and second time to Pos 0, and change from 0 to 1 againg and back to 0 next time. (interchangeable)
variabel = 'test';
LW= 'azyxwvutsrqponmlkjihgfedcb';
MW= 'acedfhgikjlnmoqprtsuwvxzyb';
RW= 'abcdefghijklmnopqrstuvwxyz';
dW='';
notFound = 1;
Pos=1;
for p=1:length(variabel)
while notFound == 1
disp('we are in the while-loop');
if Pos == 1
if strcmp(LW(1),variabel(p))
disp('Is in pos 1!!\n');
dW= append(dW, RW(1));
notFound = 0;
Pos=0;
else
disp('I turn the wheels one step-Pos == 1');
RW = circshift(RW,-1); % does the same thing as above
disp(RW);
%counterclockwise
MW = circshift(MW,1); % does the same thing as above
disp(LW);
%clockwise
LW = circshift(LW,-1); % does the same thing as above
disp(LW);
end
else
if strcmp(MW(1),variabel(p))
disp('is in pos 0!!\n');
dW= append(dW, RW(1));
notFound = 0;
Pos=1;
else
disp('I turn the wheels one step-Pos == 0');
RW = circshift(RW,-1); % does the same thing as above
disp(RW);
%counterclockwise
MW = circshift(MW,1); % does the same thing as above
disp(LW);
%clockwise
LW = circshift(LW,-1); % does the same thing as above
disp(LW);
end
end
end
end

採用された回答

DGM
DGM 2021 年 9 月 11 日
編集済み: DGM 2021 年 9 月 11 日
There are a few problems. LW(1) will never match variabel, since they're different lengths. The first 3 characters of LW will never match variabel, since all that's going on here are circular shifts, and LW does not contain that substring.
pT='abc';
variabel = pT;
LW= 'azyxwvutsrqponmlkjihgfedcb';
MW= 'acedfhgikjlnmoqprtsuwvxzyb';
RW= 'abcdefghijklmnopqrstuvwxyz';
dW='';
notFound = 1;
Pos=1;
while notFound == 1
%disp('we are in the while-loop');
if Pos == 1
% Pos will never be changed
if strcmp(LW(1:numel(variabel)),variabel) % need to test at least the same number of chars
%disp('Is in pos 1!!\n');
dW= append(dW, RW(1));
notFound = 0;
Pos = 0;
else
%disp('I turn the wheels one step-Pos == 1');
% store=RW;
% for j=1:(length(RW)-1)
% RW(j)=RW(j+1);
% end
% RW(end)=store(1);
RW = circshift(RW,-1); % does the same thing as above
% store=MW;
% store(1)=MW(end);
% for m=1:(length(MW))
% store(m+1)=MW(m);
% end
% MW=store(1:end-1);
MW = circshift(MW,1); % does the same thing as above
% store=LW;
% for n=1:(length(LW)-1)
% LW(n)=LW(n+1);
% end
% LW(end)=store(1);
LW = circshift(LW,-1); % does the same thing as above
end
else
% all this can be simplified in a similar manner
end
end
Even if the substring under test is something like [LW(1) MW(1) RW(1)], that won't match variabel either, since LW and RW are shifted in the same direction and no occurrence of ['a' MW(1) 'c'] will ever occur.
As the comment mentions, the second part simplifies as well. Depending on what the actual goals are, the entire thing should simplify further yet. For example, if Pos always is initialized to 1, then the whole second part of the loop is unused and can be removed. This is because the only time Pos is set to 0 is when the loop exits.
pT='abc';
variabel = pT;
LW= 'azyxwvutsrqponmlkjihgfedcb';
MW= 'acedfhgikjlnmoqprtsuwvxzyb';
RW= 'abcdefghijklmnopqrstuvwxyz';
dW='';
notFound = 1;
while notFound == 1
if strcmp(LW(1:numel(variabel)),variabel) % need to test at least the same number of chars
dW= append(dW, RW(1));
notFound = 0;
else
RW = circshift(RW,-1);
MW = circshift(MW,1);
LW = circshift(LW,-1);
end
end
But of course, I don't know what this is really supposed to be doing.
  4 件のコメント
Nicle Davidson
Nicle Davidson 2021 年 9 月 12 日
Thank you, your code writing is very good.

サインインしてコメントする。

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by