For-loops questions
1 回表示 (過去 30 日間)
古いコメントを表示
Use a switch to create a function with the following header:
function [ vowels ] = findVowels ( charCell )
where: charCell is a two-dimensional cell array and each cell contains a character array and vowels is a cell array with the same dimensions as charCell that contains only the vowels of each entry, in lower case.
Your function should be able to reproduce the following test case:
>> a = { ’ Wenyu ’ , ’Li ’; ’ Michael ’ , ’ Frenklach ’}
a =
2x2 cell array
‘Wenyu ’ ‘Li ’
‘Michael ’ ‘Frenklach ’
>> findVowels (a)
ans =
2x2 cell array
‘eu ’ ‘i’
‘iae ’ ‘ea'
My answer:
function [ vowels ] = findVowels( charCell )
string st = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
a = st[Random.Range(st.Length)];
charCell = {a1, a2; a3, a4};
switch vowles(letter)
case {'a' 'e''i''o''u'}
a = {'letter1','letter2';'letter3','letter4'};
otherwise
disp a = []
end
What step did I mess up?Thanks a lot
0 件のコメント
回答 (1 件)
Walter Roberson
2017 年 10 月 14 日
編集済み: Walter Roberson
2017 年 10 月 14 日
You have
string st = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
In MATLAB, that is equivalent to calling
string('st', '=', "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
This will get you the error,
No constructor 'string' with matching signature found.
MATLAB does not put the variable type before the variable name when an assignment is made. Leave out the variable type: just use
st = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
You have
st.Length
However, there is no Length method for MATLAB string objects. MATLAB string objects are arrays of strings, so if you want the length of one element of the array you need to extract out that one element and ask for its length:
length(st{1})
In the case of a string array, if you wanted to find the length of every element, you could use
cellfun(@length, st)
but if you do that, watch out that you might be wanting a different random position for each string in the string array.
You have
Random.Range(st.Length)
MATLAB does not have any package named "Random" with a static method named "Range". You kind of look like you are using Java, but Java does not have that either. https://www.mkyong.com/java/java-generate-random-integers-in-a-range/ . Consider using randi()
You have
a = st[Random.Range(st.Length)];
In MATLAB, [] is only for building lists and arrays, and is not used for indexing. Also, because string objects are arrays, if you try to just index, you would be indexing which string rather than indexing into a string.
You have two choices:
1)
pos = ... the random location you determined from above
a = extractBetween(st, pos, pos);
This will extract the given position from each string in the string array, giving back a string array. Or,
2)
st{1}(pos)
this will extract the given position from the first string in the string array.
You have
charCell = {a1, a2; a3, a4};
However, you have not defined a1, a2, a3, or a4.
You have
switch vowles(letter)
However, you have not defined "vowles" or "letter". Does "vowles" have anything to do with "vowels" ?
.... I would suggest that you read https://www.mathworks.com/matlabcentral/answers/360652-display-the-characters-as-output
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!