MATLAB Answers

for loop that changes specific letters to numbers

30 ビュー (過去 30 日間)
Seaturtle
Seaturtle 2019 年 10 月 2 日
コメント済み: Adam Danz 2019 年 10 月 4 日
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

  0 件のコメント

サインインしてコメントする。

採用された回答

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 件のコメント

表示 4 件の古いコメント
Seaturtle
Seaturtle 2019 年 10 月 3 日
I ended up making the changes myself with the original answer that you posted, so it worked out great. Thank you!
Guillaume
Guillaume 2019 年 10 月 3 日
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.
Adam Danz
Adam Danz 2019 年 10 月 3 日
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 件のコメント

表示 5 件の古いコメント
Walter Roberson
Walter Roberson 2019 年 10 月 3 日
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 日
Thanks for this answer! This really helped with my understanding of not using a loop to do something with shorter code.
Adam Danz
Adam Danz 2019 年 10 月 3 日
Glad I could chip in! :)

サインインしてコメントする。


Jos (10584)
Jos (10584) 2019 年 10 月 3 日
Another option:
str = 'apple';
TF1 = any(lower(str) ~= 'aeiou'.')

  6 件のコメント

表示 3 件の古いコメント
Walter Roberson
Walter Roberson 2019 年 10 月 3 日
TF1 = all(lower(str) ~= 'aeiou'.')
Jos (10584)
Jos (10584) 2019 年 10 月 4 日
Thanks for the corrections :-)
Adam Danz
Adam Danz 2019 年 10 月 4 日
+1
This implicit expansion solution is faster and neater than my ismember() solution.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by