1 view (last 30 days)
Lmm3 on 12 Sep 2017
Edited: Jan on 14 Sep 2017
In the code below, for b = 'AGA' the ouput is c = 'S'. Why isn't the output c = 'R'?
b = 'AGA';
if b=='UUU' | b=='UUC'
c = 'F'
elseif b =='UCU' | b=='UCC' | b=='UCA' | b=='UCG' | b=='AGC' | b=='AGU'
c = 'S'
elseif b =='CGU' | b=='CGC' | b=='CGA' | b=='CGG' | b=='AGA' | b=='AGG'
c = 'R'
end

Stephen23 on 12 Sep 2017
Edited: Stephen23 on 12 Sep 2017
Explanation
"...the ouput is c = 'S'. Why isn't the output c = 'R'?"
Because your code does not do what you think it does.
The reason why your code does not work is explained in the documentation, which is why you should always read the documentation for every operation and function that you use. Lets have a look at why your code does not work. The reason why c = 'S' is returned is clearly because the elements of the first elseif condition are all true, exactly as the if documentation states. Lets have a look at the if condition to check:
>> b = 'AGA';
>> b =='UCU' | b=='UCC' | b=='UCA' | b=='UCG' | b=='AGC' | b=='AGU'
ans =
1 1 1
And the reason why the elements are all true is because you decided to use the logical or |, which works element-wise on arrays. In fact only two of the terms are required to get this effect:
>> b=='UCA' | b=='AGC'
ans =
1 1 1
because each of these terms separately gives this:
>> b=='UCA'
ans =
0 0 1
>> b=='AGC'
ans =
1 1 0
and clearly the or of those two vectors is one vector with all true, and thus the first elseif contains only true, and so c = 'S'.
Basically you used the wrong commands, did not bother to read the documentation, and did not check the output from the code that you wrote. These are common mistakes that beginners make: they rely on what they imagine/want/wish/believe their code is doing, and never actually check what the code is really doing. Code cannot read your mind, and does not care about what you want. It is your job to read the documentation and check each line of code to ensure that it is doing exactly what you want.
Better Solutions
MATLAB is a beautiful high-level language, so please do not write ugly code as if this was an ugly low-level language like C++, where everything has to be solved using loops and ifs. As Walter Roberson stated, you should really use strcmp and short-circuit logical operators, or simply use ismember:
b = 'AGA';
if ismember(b,{'UUU','UUC'})
c = 'F';
...
end
or even simpler would be to use switch:
b = 'AGA';
switch b
case {'UUU','UUC'}
c = 'F';
...
end
Jan on 14 Sep 2017
+1. The actual question "Why isn't the output c = 'R'?" is explained in this answer and better methods are suggested.

Walter Roberson on 12 Sep 2017
Edited: Walter Roberson on 12 Sep 2017
You should be using strcmp to compare strings.
You should also consider using ismember

John BG on 12 Sep 2017
Hi Lmm3
this is John BG jgb2012@sky.com
Why not simply using a switch?
b = 'AGA';
switch b
case {'UUU' 'UUC'}
c = 'F';
case {'UCU' 'UCC' 'UCA' 'UCG' 'AGC' 'AGU'}
c = 'S';
case {'CGU' 'CGC' 'CGA' 'CGG' 'AGA' 'AGG'}
c = 'R';
otherwise
c='not defined';
end
Despite the high level compactness of the suggested commands ismember and repelem with the need of defining anonymous functions, such commands end up calling the 'low level' ugly ones that end up doing the hard work. High level functions are passengers carried by low level working functions.
The switch allows an immediate visual inspection of the different logical conditions paired with the expected output.
John BG
Jan on 14 Sep 2017
Edited: Jan on 14 Sep 2017
Stephen has suggested the solution with switch clearly in the "Better Solutions" section already with one example for a case expression. Adding the other strings as further cases is not really new.