Help with displaying the vowels
11 views (last 30 days)
Show older comments
Caleb Steel
on 10 Oct 2017
Commented: Rena Berman
on 30 Oct 2017
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
Accepted Answer
Walter Roberson
on 10 Oct 2017
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 Comments
Walter Roberson
on 10 Oct 2017
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
on 11 Oct 2017
The solution for in-order output of the vowels is:
vowels = 'aeiouàáâãäåæèéêìíîïòóôõöùúûüýāăąőűǻǽǿȺḯṍấắếớứ';
disp(cellfun(@(C) C(ismember(C,vowels)), lower(Test), 'uniform', 0))
More Answers (1)
dpb
on 10 Oct 2017
Edited: dpb
on 10 Oct 2017
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 Comments
Walter Roberson
on 11 Oct 2017
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.
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!