Help with displaying the vowels
조회 수: 4 (최근 30일)
이전 댓글 표시
Hi, I want to make my code to be converted to switch and case statements and have the desired output from the picture:

function [vowels] = locateVowels( charactercell )
isvowel=@(s) ismember(lower(s),'aeiou');
isvowel=@(s) ismember(lower(s),'aeiou');
idxV=cellfun(isvowel,charactercell,'unif',0);
vowels=cellfun(@(s,i) lower(s(i)),charactercell,idxV,'unif',0);
end
채택된 답변
Walter Roberson
2017년 10월 10일
Hint:
x(ismember(x, y))
However, it is tricky to determine what the vowels are in English. See https://www.mathworks.com/matlabcentral/answers/326767-finding-vowels-in-a-string-the-user-inputs-and-printing-the-vowels-here-s-the-problem#answer_256208
댓글 수: 3
Walter Roberson
2017년 10월 10일
vowels = 'aeiouàáâãäåæèéêìíîïòóôõöùúûüýāăąőűǻǽǿȺḯṍấắếớứ';
cellfun(@(s) vowels(ismember(vowels, lower(s))), test, 'uniform', 0)
The vowels list above is not complete; I got tired of going through the charts. Also, it is not impossible that some of those might have the shape of a vowel with a combining mark but might, in their respective language, not act as a vowel.
I did not attempt to sort the vowels alphabetically: they are sorted above by unicode position. For example in Polish, ą sorts immediately after a in the dictionary
Walter Roberson
2017년 10월 11일
The solution for in-order output of the vowels is:
vowels = 'aeiouàáâãäåæèéêìíîïòóôõöùúûüýāăąőűǻǽǿȺḯṍấắếớứ';
disp(cellfun(@(C) C(ismember(C,vowels)), lower(Test), 'uniform', 0))
추가 답변 (1개)
dpb
2017년 10월 10일
편집: dpb
2017년 10월 10일
Use the string a cell array processing features of Matlab..
test={'Opal','Otis';'Fib','Lupe'};
isvowel=@(s) ismember(lower(s),'aeiou');
idxV=cellfun(isvowel,test,'unif',0);
cellfun(@(s,i) lower(s(i)),test,idxV,'unif',0)
ans =
'oa' 'oi'
'i' 'ue'
Presuming this is homework, I recommend you fully understand how it works and can reproduce it without coaching/help if asked to 'splain how you got the solution... :)
Hopefully will provide some ideas on how to more efficiently find the locations and you can then build your own solution based on those...or, if is real problem and not homework, you're more than welcome!! :)
댓글 수: 12
Walter Roberson
2017년 10월 11일
function [vowels] = locateVowels( charactercell )
vowels = cell(size(charactercell));
for cell_idx = 1 : numel(charactercell)
thesevowels = '';
vowelcount = 0;
thisentry = charactercell{cell_idx};
for str_idx = 1 : length(thisentry)
switch lower(thisentry(str_idx))
case 'a'
vowelcount = vowelcount + 1;
thesevowels(vowelcount) = 'a';
case 'ä'
vowelcount = vowelcount + 1;
thesevowels(vowelcount) = 'ä';
case 'e'
vowelcount = vowelcount + 1;
thesevowels(vowelcount) = 'e';
case 'ë'
vowelcount = vowelcount + 1;
thesevowels(vowelcount) = 'ë';
case 'é'
vowelcount = vowelcount + 1;
thesevowels(vowelcount) = 'é';
case 'i'
vowelcount = vowelcount + 1;
thesevowels(vowelcount) = 'i';
case 'ï'
vowelcount = vowelcount + 1;
thesevowels(vowelcount) = 'ï';
case 'o'
vowelcount = vowelcount + 1;
thesevowels(vowelcount) = 'o';
case 'ö'
vowelcount = vowelcount + 1;
thesevowels(vowelcount) = 'ö';
case 'u'
vowelcount = vowelcount + 1;
thesevowels(vowelcount) = 'u';
case 'ü'
vowelcount = vowelcount + 1;
thesevowels(vowelcount) = 'ü';
end
end
vowels{cell_idx} = thesevowels;
end
end
This is an approach I would never take, and is suitable only for an exercise in proving that one knows how to use the switch construct and cell arrays.
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!