how to: multiple choises in if-function
조회 수: 1 (최근 30일)
이전 댓글 표시
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
Andreas Goser
2023년 3월 7일
Sie können Fragen gerne in Deutsch stellen. Es wird eine Mischung aus deutschen und englischen Antworten kommen.
답변 (1개)
Jonas
2023년 3월 6일
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"])
% or at least, which is a bot more compact
ismember(i,[1 5 9 15 21])
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!