# Problem with if statements

조회 수: 1(최근 30일)
Blake 2022년 11월 21일
댓글: David Hill 2022년 11월 28일
Hey!
Whenever I run this script, the answer polar is always displayed even though the answer should be nonpolar. Any help would be apprecitated. Thank you.
H = 2.20;
Li = 0.98;
Be = 1.57;
B = 2.04;
C = 2.55;
N = 3.04;
O = 3.44;
F = 3.98;
Na = 0.93;
Mg = 1.31;
Al = 1.61;
Si = 1.90;
P = 2.19;
S = 2.58;
Cl = 3.16;
K = 0.82;
Ca = 1;
Se = 1.36;
Ti = 1.54;
V = 1.63;
Cr = 1.66;
Mn = 1.55;
Fe = 1.83;
Co = 1.88;
Ni = 1.91;
Cu = 1.90;
Zn = 1.65;
Ga = 1.81;
Ge = 2.01;
As = 2.18;
Se = 2.55;
Br = 2.96;
FirstElement = input('What is the first element in the compound?');
SecondElement = input('What is the second element in the compound?');
Polarity = SecondElement - FirstElement;
while Polarity == SecondElement - FirstElement;
if FirstElement == Li, SecondElement == H;
Polarity = H - Li;
end
if FirstElement == Be, SecondElement == H;
Polarity = H - Be;
end
if FirstElement == B, SecondElement == H;
Polarity = H - B;
end
if FirstElement == B, SecondElement == H;
Polarity = H - B;
end
if FirstElement == B, SecondElement == C;
Polarity = C - B;
end
if FirstElement == B, SecondElement == N;
Polarity = N - B;
end
if FirstElement == B, SecondElement == O;
Polarity = O - B;
end
if FirstElement == B, SecondElement == F;
Polarity = F - B;
end
end
disp(ans(Polarity))
if Polarity > 0.4;
disp('Polar')
elseif disp('Non Polar')
end

댓글을 달려면 로그인하십시오.

### 채택된 답변

David Hill 2022년 11월 21일
e=["H","Li","Be","B","C","N","O","F","Na","Mg","Al","Si","P","S","Cl","K","Ca",...
"Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As","Se","Br"];
l=[2.20;0.98;1.57;2.04;2.55;3.04;3.44;3.98;0.93;1.31;1.61;1.90;2.19;2.58;3.16;...
0.82;1;1.36;1.54;1.63;1.66;1.55;1.83;1.88;1.91;1.90;1.65;1.81;2.01;2.18;...
2.55;2.96];
FirstElement = find(ismember(e,string(input('What is the first element in the compound?','s'))));
SecondElement = find(ismember(e,string(input('What is the second element in the compound?','s'))));
Polarity = abs(l(SecondElement) - l(FirstElement));
if Polarity > 0.4
disp('Polar')
else
disp('Non Polar')
end
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
David Hill 2022년 11월 28일
ismember is producing a logical array the same size as e. You could also do e=="Cr" which produces the same logical array. abs is just the absolute value of the subtraction. input will produce a character array but we need to compare string to string. string just converts the character array into a string.
e=["H","Li","Be","B","C","N","O","F","Na","Mg","Al","Si","P","S","Cl","K","Ca",...
"Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As","Se","Br"];
ismember(e,"Cr")
ans = 1×32 logical array
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
e=="Cr"
ans = 1×32 logical array
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0

댓글을 달려면 로그인하십시오.

### 추가 답변(1개)

Steven Lord 2022년 11월 21일
편집: Steven Lord 2022년 11월 21일
Don't define a large number of variables whose names match the symbols for the elements and use a large collection of if statements. Instead I recommend using either a struct array with dynamic field names or a table with named rows.
polarityValues = struct(H = 2.20, Li = 0.98, Be = 1.57, B = 2.04)
polarityValues = struct with fields:
H: 2.2000 Li: 0.9800 Be: 1.5700 B: 2.0400
Or if you have too many fields to set this way, you could define them with lines like:
polarityValues.C = 2.55
polarityValues = struct with fields:
H: 2.2000 Li: 0.9800 Be: 1.5700 B: 2.0400 C: 2.5500
Now if we want to retrieve the polarityValues for two elements:
firstElement = 'Li';
secondElement = 'Be';
polarity(1) = polarityValues.(firstElement);
polarity(2) = polarityValues.(secondElement)
polarity = 1×2
0.9800 1.5700
For a table-based approach:
elementSymbols = ["H"; "Li"; "Be"; "B"; "C"];
polarityValues = [2.2; 0.98; 1.57; 2.04; 2.55];
t = table(polarityValues, 'VariableNames', "Polarity", 'RowNames', elementSymbols)
t = 5×1 table
Polarity ________ H 2.2 Li 0.98 Be 1.57 B 2.04 C 2.55
To retrieve values:
polarity = t{'Li', 'Polarity'}
polarity = 0.9800
polarity = t{{firstElement, secondElement}, 'Polarity'}
polarity = 2×1
0.9800 1.5700
This neatly avoids the problem where you don't have an if statement for a particular combination of elements, in which case the polarity variable will be defined as the difference between the symbols of the elements due to your line "Polarity = SecondElement - FirstElement;"
polarity = firstElement - secondElement
polarity = 1×2
10 4
L is 10 characters later in the alphabet than B and i is 4 characters later in the alphabet than e. Because in this case polarity is a vector, the body of that last if statement in your code will be executed if all the elements of the vector are greater than 0.4, and in this case they are.
##### 댓글 수: 1표시숨기기 없음
Blake 2022년 11월 24일
This is amazing and extremely helpful, thank you so much.

댓글을 달려면 로그인하십시오.

### 범주

Find more on Structures in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!