for loop that changes specific letters to numbers

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

 채택된 답변

Walter Roberson
Walter Roberson 2019년 10월 2일
party = input('What is your answer? ', 's');
n = length(party)
for i = 1:n
if party(i) == 'A'
party(i) = 0;
elseif i == 'B'
party(i) = 1;
elseif i == 'C'
party(i) = 1;
end
end
disp( double(party) )

댓글 수: 7

The conditions following the initial one all contain the index value i in the antecedent.
Instead, it should be
for i = 1:n
if party(i) == 'A'
party(i) = 0;
elseif party(i) == 'B'
party(i) = 1;
elseif party(i) == 'C'
party(i) = 1;
end
end
Is the idea to create a conditional statement for each individual character in both upper and lower case, presumably? That would result in over 100 conditions. An improvement would be to use lower() or upper() which would eliminate 26 of those conditions. Why not just use the non-loop, 1-liner?
~isstrprop(party, 'vowel')
It would be nice if 'vowel' were a category option.
party = 'apple';
~isstrprop(party, 'vowel')
Error using isstrprop
vowel is an invalid qualifier.
r2019b
Atch, I was getting questions confused, as someone recently asked about upper vs lower case.
I ended up making the changes myself with the original answer that you posted, so it worked out great. Thank you!
It would be nice if 'vowel' were a category option.
The problem with that is what is a vowel or not depends on the language. Some letters such as 'y' in english can also qualify as a vowel or consonant depending on the word.
True. It would be neat to play around with one of the vowel classification algorithms that are not specific to any language such as this one (link below) that is based on character co-occurrences. It wouldn't necessarily solve the fuzzy set problem but it would be a semi-objective, language-independent classification.

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

추가 답변 (2개)

Adam Danz
Adam Danz 2019년 10월 2일
편집: Adam Danz 2019년 10월 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

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]
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.
+1 very neat solution
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.
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 :)
semi-vowel does sound like the correct term.
In practice, y probably acts like a vowel more than it acts like a consontant .
Seaturtle
Seaturtle 2019년 10월 3일
편집: Seaturtle 2019년 10월 3일
Thanks for this answer! This really helped with my understanding of not using a loop to do something with shorter code.
Glad I could chip in! :)

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

Jos (10584)
Jos (10584) 2019년 10월 3일
Another option:
str = 'apple';
TF1 = any(lower(str) ~= 'aeiou'.')

댓글 수: 6

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.)
TF1 = any(lower('apple') ~= 'aeiou'.')
TF1 =
1×5 logical array
1 1 1 1 1
Adam Danz
Adam Danz 2019년 10월 3일
편집: Adam Danz 2019년 10월 3일
I think what you meant was,
TF = ~any(lower('apple') == 'aeiou'.')
TF1 = all(lower(str) ~= 'aeiou'.')
Thanks for the corrections :-)
+1
This implicit expansion solution is faster and neater than my ismember() solution.

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

카테고리

도움말 센터File Exchange에서 Entering Commands에 대해 자세히 알아보기

질문:

2019년 10월 2일

댓글:

2019년 10월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by