This is MATLAB Create three cell array variables that store people\'s names, ver
ID: 3864739 • Letter: T
Question
This is MATLAB
Create three cell array variables that store people's names, verbs, and nouns. For example, names = {'Harry', 'Xavier', 'Sue'}; verbs = {'likes', 'eats', 'makes'}; nouns = {'baseballs', 'rocks', 'sushi'}; Write a script that will initialize these cell arrays in the variables "names", "verbs", and "nouns" and will create a sentence (stored in the variable "sentence") using one random element from each cell array (e.g. "Xavier eats baseballs"). Every element must be a single word. For example, "fish" is a valid element of the nouns list but "fried clams" is not.
Explanation / Answer
Answer:-
matlab code:-
note:- code is explained with comments followed by the symbol %.
names = {'Harry', 'Xavier', 'Sue'}; % initializing cell array names.
verbs = {'likes', 'eats', 'makes'}; % initializing cell array verbs.
nouns = {'baseballs', 'rocks', 'sushi'}; % initializing cell array nouns.
sentence = strcat(names(randi([1,3])),{' '},verbs(randi([1,3])),{' '},nouns(randi([1,3])))
% concatenating random elements from cell arrays names,verbs and nouns with spaces in between them by using %strcat() function.
% to generate spaces between words {' '} is used.
% since each cell array contains 3 values random numbers between 1 to 3 are generated using randi([a,b]) function. %to get values between 1 and 3 where a=1 and b=1.
output of the above matlab code when run for multiple times:-
>> cellarray
sentence =
''Xavier likes baseballs''
>> cellarray
sentence =
''Harry makes baseballs''
sentence =
''Sue makes sushi''
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.