If statement not working correctly
Mostra commenti meno recenti
This is driving me insane, I've checked the code 101 times yet can't find any error. This a simple application of if statement.
for i=1:c
parity=mod(x(i),2);
d=bistr(i);
if parity==1 && d==0
display('Hello');
elseif parity==0 && d==0
display('How');
elseif parity==0 && d==1
display('Are');
elseif parity==1 && d==1
display('You');
end
end
i goes from 1 to 192 (c=192). As you can see, parity can either be 0 or 1. d is also either 0 or 1 (it's a matrix of 1s and 0s)
YET THIS CODE WON'T PRINT ANY SINGLE OF THE STRING 'HELLO','HOW','ARE' or 'YOU'. Like I've exhausted all the logical possibilities for parity and d. What is going wrong?
(Yes I've tried printing every single variable at the starting of the loop to see if the loop itself is working fine. IT IS. IT'S JUST THAT THE IF STATEMENT ISN'T GETTING EXECUTED AT ALL!)
3 Commenti
"If statement not working correctly"
Yes, it is. You are not providing values that fulfill those conditions. This is not if's fault.
You have not told us what x is, but if it is floating point and contains any small fractional values then the equivalence statements will always be false. Note that MATLAB will not display small fractional part for an integer, unless you change the format:
>> 1.0000000000006
ans = 1.0000
>> format longg
>> 1.0000000000006
ans = 1.0000000000006
Also the name bistr indicates that it is a string: note that the string '1' does not have a value of one!:
>> double('1')
ans = 49
Please tell us how you are generating x and bistr, and also upload examples of these variables in one mat file (change file extension to txt) by clicking the paperclip button.
Adam
il 3 Ago 2016
What is 'bistr'?
Presumably whatever it is returning is not actually either a 0 or a 1
Arnav Barbaad
il 3 Ago 2016
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!