Help with displaying the vowels

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

 Respuesta aceptada

Walter Roberson
Walter Roberson el 10 de Oct. de 2017

3 comentarios

Caleb Steel
Caleb Steel el 10 de Oct. de 2017
I'm not sure how to use the hint you have given me.
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
The solution for in-order output of the vowels is:
vowels = 'aeiouàáâãäåæèéêìíîïòóôõöùúûüýāăąőűǻǽǿȺḯṍấắếớứ';
disp(cellfun(@(C) C(ismember(C,vowels)), lower(Test), 'uniform', 0))

Iniciar sesión para comentar.

Más respuestas (1)

dpb
dpb el 10 de Oct. de 2017
Editada: dpb el 10 de Oct. de 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 comentarios

Caleb Steel
Caleb Steel el 10 de Oct. de 2017
This is a real problem. Also, I am not too familiar with coding, especially dealing with functions that involve cell arrays.
dpb
dpb el 10 de Oct. de 2017
Editada: dpb el 10 de Oct. de 2017
OK, does the above not then solve the problem in its entirety (other than as Walter show, the list of potential vowel characters is language dependent so have to fix that up to match your problem space)?
The difference in the two is that Walter's gives the vowels in each cell in the order in which they're found in the list of vowels; mine returns them in the order they're found in the input.
Just wrap the above in a function wrapper to pass in the input array...
function res=vowels(cellstrarray)
isvowel=@(s) ismember(lower(s),'aeiou');
idxV=cellfun(isvowel,cellstrarray,'unif',0);
res=cellfun(@(s,i) lower(s(i)),cellstrarray,idxV,'unif',0);
If the list of vowels isn't fixed, then could easily add it as an (optional) second parameter.
Caleb Steel
Caleb Steel el 10 de Oct. de 2017
Editada: Caleb Steel el 10 de Oct. de 2017
The above code you gave me works fine for me, but I would like to see the code involving switch and case.
You cannot just disp() those things. disp() displays them in the command window. You need to store them, like
counter = 0;
switch cell{1:end,2}
case 'a'
counter = counter + 1;
output(counter) = 'a';
case 'e'
counter = counter + 1;
output(counter) = 'e';
end
Then once you have stored them all in the order you want, you can display the output.
Note: do not use "cell" as the name of a variable: you will intefere with the important function cell()
Note: you had the wrong syntax for switch.
Note: really, though, the line
cellfun(@(s,i) lower(s(i)),test,idxV,'unif',0)
is going to display the results anyhow, because you did not assign the result to a variable and you did not put a semi-colon on the end of the line to suppress the output.
Note: really, though, all you need to do is assign the output of that cellfun to a variable and disp() the variable. The default output when you just allow a value to be displayed is different than if you disp() the variable. dpb happens to be running an older version which naturally output in the way you would prefer.
Caleb Steel
Caleb Steel el 10 de Oct. de 2017
Editada: Caleb Steel el 10 de Oct. de 2017
So far I have the below code but "cell" has red lines under it.
function [vowels] = locateVowels( charactercell )
vowels = 0;
switch cell{1:end,2}
case 'a'
charactercell = charactercell + 1;
vowels(charactercell) = 'a';
case 'e'
charactercell = charactercell + 1;
vowels(charactercell) = 'e';
case 'i'
charactercell = charactercell + 1;
vowels(charactercell) = 'i';
case 'o'
charactercell = charactercell + 1;
vowels(charactercell) = 'o';
case 'u'
charactercell = charactercell + 1;
vowels(charactercell) = 'u';
end
end
dpb
dpb el 10 de Oct. de 2017
"...I would like to see the code involving switch and case."
Why!?? switch construct has its place, but this really isn't it...just make more work than needs be.
Walter Roberson
Walter Roberson el 10 de Oct. de 2017
You do not return charactercell from the function, and you do not display it, so the editor is warning you that you are wasting time in computing it.
Are you sure you want to be writing over the top of your input ?
Are you sure that you want to be using a shared variable named "cell", thereby confusing the heck out of the people who are trying to read your code? Most of whom would assume that there "cell" is trying to refer to the function cell(), or else that "cell" is referring there to an uninitialized variable?
If the point is only to count the vowels then why bother storing them?
Caleb Steel
Caleb Steel el 10 de Oct. de 2017
Editada: Caleb Steel el 10 de Oct. de 2017
When I do not include "cell", "end" is underlined.
function [vowels] = locateVowels( charactercell )
vowels = 0;
switch {1:end,2}
case 'a'
charactercell = charactercell + 1;
vowels(charactercell) = 'a';
case 'e'
charactercell = charactercell + 1;
vowels(charactercell) = 'e';
case 'i'
charactercell = charactercell + 1;
vowels(charactercell) = 'i';
case 'o'
charactercell = charactercell + 1;
vowels(charactercell) = 'o';
case 'u'
charactercell = charactercell + 1;
vowels(charactercell) = 'u';
end
end
Walter Roberson
Walter Roberson el 10 de Oct. de 2017
Editada: Walter Roberson el 10 de Oct. de 2017
Sigh.
function [vowels] = locateVowels( charactercell )
vowels = '';
vowelcount = 0;
for row = 1 : length(charactercell)
thisentry = charactercell{row, 2};
switch thisentry
case 'a'
vowelcount = vowelcount + 1;
vowels(vowelcount) = 'a';
case 'ä'
vowelcount = vowelcount + 1;
vowels(vowelcount) = 'ä';
case 'e'
vowelcount = vowelcount + 1;
vowels(vowelcount) = 'e';
case 'ë'
vowelcount = vowelcount + 1;
vowels(vowelcount) = 'ë';
case 'é'
vowelcount = vowelcount + 1;
vowels(vowelcount) = 'é';
case 'i'
vowelcount = vowelcount + 1;
vowels(vowelcount) = 'i';
case 'ï'
vowelcount = vowelcount + 1;
vowels(vowelcount) = 'ï';
case 'o'
vowelcount = vowelcount + 1;
vowels(vowelcount) = 'o';
case 'ö'
vowelcount = vowelcount + 1;
vowels(vowelcount) = 'ö';
case 'u'
vowelcount = vowelcount + 1;
vowels(vowelcount) = 'u';
case 'ü'
vowelcount = vowelcount + 1;
vowels(vowelcount) = 'ü';
end
end
end
Note: all of the above are used in English. https://en.wikipedia.org/wiki/English_terms_with_diacritical_marks
Caleb Steel
Caleb Steel el 11 de Oct. de 2017
Editada: Caleb Steel el 11 de Oct. de 2017
When I try to run this code, the output results in 0x0 char array. I want the output to be a 2x2 cell array
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 el 11 de Oct. de 2017
Thank you so much!

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Preguntada:

el 10 de Oct. de 2017

Comentada:

el 30 de Oct. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by