For-loops questions

2 次查看(过去 30 天)
Meowooo
Meowooo 2017-10-13
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

回答(1 个)

Walter Roberson
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" ?

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by