Help with displaying the vowels
5 ビュー (過去 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!