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
1 件のコメント
Rena Berman
2017 年 10 月 30 日
(Answers Dev) Restored edit
採用された回答
その他の回答 (1 件)
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 件のコメント
Caleb Steel
2017 年 10 月 10 日
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
2017 年 10 月 10 日
編集済み: Caleb Steel
2017 年 10 月 10 日
Walter Roberson
2017 年 10 月 10 日
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
2017 年 10 月 10 日
編集済み: Caleb Steel
2017 年 10 月 10 日
dpb
2017 年 10 月 10 日
"...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
2017 年 10 月 10 日
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
2017 年 10 月 10 日
編集済み: Caleb Steel
2017 年 10 月 10 日
Walter Roberson
2017 年 10 月 10 日
編集済み: Walter Roberson
2017 年 10 月 10 日
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
2017 年 10 月 11 日
編集済み: Caleb Steel
2017 年 10 月 11 日
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.
Caleb Steel
2017 年 10 月 11 日
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!