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

 Accepted Answer

DGM
DGM on 11 Sep 2021
Edited: DGM on 11 Sep 2021
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 Comments

First of all, thank you for making me aware of circshift(), it looks very much more effective than what I had tried to code for it.
then, if strcmp(LW(1:numel(variabel)),variabel) still is not able to do the check, even when variable is 'a' and the first word is 'a' in the string of words.
So I understand that the problem exists in the 'if' statement,
so I think I should ask how could I compare this so it could compare the first word with another string.
Say I have a string called pT= 'test'
and I have a string such as shown above like: LW= 'azyxwvutsrqponmlkjihgfedcb';
As I knew before strcmp(,) would have been able to do this
but now I see there is some complications.
I think I should go a bit back in the code (which is not long) to tell the history of LW
I have the strings:
pT='test';
fS = 'abcdefghijklmnopqrstuvwxyz';
sS = 'acedfhgikjlnmoqprtsuwvxzyb';
vS = 'azyxwvutsrqponmlkjihgfedcb';
spokes = perms({fS, sS, vS});%I permutate them
for q=1:6 %then in a for-loop of 6 times
LW= spokes(q:1);
MW= spokes(q:2);
RW= spokes(q:3);
%then I send LW, MW, RW, pT in a function to another page,
%which has the code above.
%that is where the if statement wants to comare and see
%if for example what is in LW this time, is the first word in LW now
% 't'? ( I have added the for loop for taking a word
% from pT at a time.
%please see the update of the code above
%then do the function mention above (in the first place
%I wrote the question) here
answer =anotherPage(ML, RW, LW, pT);
end
The while loop still does not stop, because notFound = 0 is in the if statement, and the if statement does not fuction, is there any solution for the functionality of the if-statement with the contition in the code I explained here?
Regarding this part of your answer:'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.'
The code is supposed to take first word from the LW and second time take the first word from the MW, third time from the LW, forth time from MW and so on, so it should change between them. that is the reason I used Pos and set it once to 1 and it goes to LW, then after it has done the job with LW, I set Pos to 0 so I want it to go and check the second word from the MW.
could I explain it better now?
Let's see if this gets us closer:
variabel = 'test';
LW= 'azyxwvutsrqponmlkjihgfedcb';
MW= 'acedfhgikjlnmoqprtsuwvxzyb';
RW= 'abcdefghijklmnopqrstuvwxyz';
dW='';
% 1 means LW; 0 means MW
% behavior should alternate between the two
Pos = 1;
for p=1:length(variabel)
% i inverted the logic here so the flag could be used for multiple things
isDone = false; % reset the flag inside the loop
while ~isDone
if Pos == 1
isDone = strcmp(LW(1),variabel(p));
else
isDone = strcmp(MW(1),variabel(p));
end
if isDone
fprintf('Finished in pos %d!!\n',Pos);
dW = append(dW, RW(1));
Pos = xor(Pos,1); % toggle
disp([RW; MW; LW])
else
fprintf('I turn the wheels one step (in pos %d)\n',Pos);
RW = circshift(RW,-1); % shift left (ccw)
MW = circshift(MW,1); % shift right (cw)
LW = circshift(LW,-1); % shift left (ccw)
end
end
end
I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1)
Finished in pos 1!!
hijklmnopqrstuvwxyzabcdefg uwvxzybacedfhgikjlnmoqprts tsrqponmlkjihgfedcbazyxwvu
I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0)
Finished in pos 0!!
yzabcdefghijklmnopqrstuvwx edfhgikjlnmoqprtsuwvxzybac cbazyxwvutsrqponmlkjihgfed
I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1)
Finished in pos 1!!
ijklmnopqrstuvwxyzabcdefgh suwvxzybacedfhgikjlnmoqprt srqponmlkjihgfedcbazyxwvut
I turn the wheels one step (in pos 0)
Finished in pos 0!!
jklmnopqrstuvwxyzabcdefghi tsuwvxzybacedfhgikjlnmoqpr rqponmlkjihgfedcbazyxwvuts
Thank you, your code writing is very good.

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!