Help with displaying the vowels

5 visualizzazioni (ultimi 30 giorni)
Caleb Steel
Caleb Steel il 10 Ott 2017
Commentato: Rena Berman il 30 Ott 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

Risposta accettata

Walter Roberson
Walter Roberson il 10 Ott 2017
  3 Commenti
Walter Roberson
Walter Roberson il 10 Ott 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
Walter Roberson il 11 Ott 2017
The solution for in-order output of the vowels is:
vowels = 'aeiouàáâãäåæèéêìíîïòóôõöùúûüýāăąőűǻǽǿȺḯṍấắếớứ';
disp(cellfun(@(C) C(ismember(C,vowels)), lower(Test), 'uniform', 0))

Accedi per commentare.

Più risposte (1)

dpb
dpb il 10 Ott 2017
Modificato: dpb il 10 Ott 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 Commenti
Walter Roberson
Walter Roberson il 11 Ott 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.
Caleb Steel
Caleb Steel il 11 Ott 2017
Thank you so much!

Accedi per commentare.

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by