Clear Filters
Clear Filters

how to: multiple choises in if-function

3 views (last 30 days)
Nils Gehrke
Nils Gehrke on 6 Mar 2023
Commented: Andreas Goser on 7 Mar 2023
hello,
first of all my english is not the yellow from the egg. I try to improve my english.
I have tried to evaluate the indice of the letter from the alphabet and determine if it is a vowal of a consonant.
This is what i have done and it works:
Erzeugen eines zufaellig ausgewaehlten Buchstaben
% Alphabet erzeugen.
alphabet = ["A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"]';
% Anzahl der Wahlmöglichkeiten bestimmen.
a = length(alphabet);
% Zufaellige Zahl waehlen.
i = randi(a);
% Zufaelligen Buchstaben waehlen.
zufBuchst = alphabet(i);
% Unterscheidung ob Konsonant oder Vokal und Darstellung, welcher Buchstabe
% es ist.
if i == 1 || i==5 || i==9 || i==15 || i==21;
disp("Der zufaellig ausgewaehlte Buchstabe lautet: '" + zufBuchst + "', steht an " + i + "ter Stelle im Alphabet und ist ein Vokal.")
else disp("Der zufaellig ausgewaehlte Buchstabe lautet: '" + zufBuchst + "', steht an " + i + "ter Stelle im Alphabet und ist ein Konsonant.")
end
But the bold part seems a little bit awkward to me.
have can i make this part shorter and easier?
  1 Comment
Andreas Goser
Andreas Goser on 7 Mar 2023
Sie können Fragen gerne in Deutsch stellen. Es wird eine Mischung aus deutschen und englischen Antworten kommen.

Sign in to comment.

Answers (1)

Jonas
Jonas on 6 Mar 2023
looks good, but you could increase readability e.g. by using the ismember function
alphabet = ["A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"]';
a = length(alphabet);
i = randi(a);
isConsonant=ismember(alphabet(i),["A" "E" "I" "O" "U"])
isConsonant = logical
0
% or at least, which is a bot more compact
ismember(i,[1 5 9 15 21])
ans = logical
0

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!