String not read correctly
Mostra commenti meno recenti
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
Risposta accettata
Più risposte (2)
Walter Roberson
il 12 Set 2017
Modificato: Walter Roberson
il 12 Set 2017
1 voto
You should be using strcmp to compare strings.
You should also consider using ismember
John BG
il 12 Set 2017
Hi Lmm3
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.
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
3 Commenti
Stephen23
il 12 Set 2017
@John BG: I suggested switch over four hours ago. Do you have something new to add?
John BG
il 12 Set 2017
To Mr Cobeldick: you suggested a switch but did not write the complete code. Although suggestions may be considered answers, as a matter of fact many are, often a hint is considered a valid answer, a complete working script is usually better appreciated as a valid answer.
You have suggested ismember ad your 1st choice of solution.
This question can be answered with different solutions.
I politely disagree that Lmm3 should consider command switch as the 2nd option.
I started writing my answer with switch in mind, as it is the simple obvious choice. It may be considered primitive, because it's at the same level of if . else, for, and while loops that many times are unreasonably deprecated against more compact ommands that many users need time to learn but it's time they primarily need focussed on achieving a working solution, not a 'beautiful' or 'ugly' one.
MATLAB coherently embraces a really wide range of commands.
Both loops and compact vectoring are equally important.
But not every body has the vectorising skills you have, or are even willing to spend the time learning how to use commands like repelem and cellfun. I am not taking sides here, just stating a fact.
Even more, considering that at the end, it's a rather narrow bunch of ASM commands that, precisely with really elemental operations, end up performing the most elevated and compact MATLAB scripts, I chose switch as the best solution to this question, regardless of whatever you have written.
Regards
John BG
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!