for loop that changes specific letters to numbers
Afficher commentaires plus anciens
I want to replace the vowels AEIOU with the number 0 and all other letters with the number 1. For example the output should be ans = [0 1 1 1 0] if the user inputs apple. I know I must be misunderstanding how to get the loop to go through my whole string. This is what I have managed so far.
party = input('What is your answer? ', 's');
n = length(party)
for i = 1:n
if i == 'A'
disp(0)
elseif i == 'B'
disp(1)
elseif i == 'C'
disp(1)
end
end
Réponse acceptée
Plus de réponses (2)
No loop needed.
str = 'apple';
isConsonant = ~ismember(lower(str),'aeiou') %lower() makes it not case sensitive
If you really wanted to do that in a loop,
n = numel(party);
isConsonant = true(1,n);
for i = 1:n
if ismember(party(i),'aeiou')
isConsonant(i) = false;
end
end
In both cases, isConsonant is a logical vector. If you want a double vector of 0/1 instead of false/true,
isConsonant = double(isConsonant);
8 commentaires
Walter Roberson
le 2 Oct 2019
The test is not for consonants: the test is for non-vowels. For example a space or period should have 1 in that location, not 0
disp() of one digit at a time does not satisfy ans = [0 1 1 1 0]
Adam Danz
le 2 Oct 2019
The no-loop version does exactly that though the variable name is misleading. The question specifies the encoding of "letters" as opposed to any character and since all non-vowel letters are consonants, the variable name would be OK if the only input were letters.
But my for-loop method indeed had issues that were fixed.
Stephen23
le 3 Oct 2019
+1 very neat solution
Walter Roberson
le 3 Oct 2019
In English, y can act like a vowel (and often does). w as well, but that is rare. There is a third letter as well like this but I keep forgetting which one.
Adam Danz
le 3 Oct 2019
J, maybe?
This solution below categorizes j, w, and y as a vowel based on a binomial random draw with a probability of being a vowel at 50% although that probability should be lowered to reflect the true frequency of those letters representing vowels :D
str = 'jabberwocky';
vowels = 'aeiou';
semiVowels = 'jwy';
isConsonant = double(~ismember(lower(str),vowels))
isSemiVowel = ismember(str,semiVowels);
isConsonant(isSemiVowel) = binornd(1, .5, [1, sum(isSemiVowel)]); %Just for fun :)
Walter Roberson
le 3 Oct 2019
semi-vowel does sound like the correct term.
In practice, y probably acts like a vowel more than it acts like a consontant .
Adam Danz
le 3 Oct 2019
Glad I could chip in! :)
Jos (10584)
le 3 Oct 2019
Another option:
str = 'apple';
TF1 = any(lower(str) ~= 'aeiou'.')
6 commentaires
Walter Roberson
le 3 Oct 2019
Cute.
(Needs R2016b or newer.)
(I wonder if I can stop saying that now that R2019b is out? I think there are still a fair number of people on older releases though.)
Adam Danz
le 3 Oct 2019
TF1 = any(lower('apple') ~= 'aeiou'.')
TF1 =
1×5 logical array
1 1 1 1 1
I think what you meant was,
TF = ~any(lower('apple') == 'aeiou'.')
Walter Roberson
le 3 Oct 2019
TF1 = all(lower(str) ~= 'aeiou'.')
Jos (10584)
le 4 Oct 2019
Thanks for the corrections :-)
Adam Danz
le 4 Oct 2019
+1
This implicit expansion solution is faster and neater than my ismember() solution.
Catégories
En savoir plus sur Entering Commands dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!