MATLAB help Write a MATLAB program genNvectors.m to prompt the user to input a p
ID: 3735543 • Letter: M
Question
MATLAB help
Write a MATLAB program genNvectors.m to prompt the user to input a positive integer N, and generate N random vectors, each with 10 random numbers uniformly distributed between 0 and 1. Each vector will be stored in a variable with a name called 'vectori'. 'vector2,' vector3 vectorN". Your program should perform error checking for the user input value of N. After testing your program, there should be N variables, vector , vectorN. created in your workspace, each with 10 random numbers. Problem 10.2 Write a MATLAB function called wordparser that can parse words for a text document. The function should take the document as input, and return one single output argument which stores all distinctive words (not case sensitive) in the document. Your program should be able to eliminate any punctuations leading or trailing each word. It should also be able to handle an empty input. Use the following input to test your program: 1. 2. 3. This is a test. Good! He said, "Go, go for it!"Explanation / Answer
Solution to Problem 10.1:
N = input("Enter value of N: ")
for i = 1 to N
%This line adds 'i' to the name of the variable (vector), and evaluates the resultant string (which is an assignment
% statement
eval(['vector' num2str(i) '=rand(10,1)'])
end
Solution to Problem 10.2:
function wordList = wordParser(docstring)
fid = fopen(docstring)
% Read all lines & collect in cell array
%fid is pointer to file, %s is format (String) and ' ' is the delimiter (The character used to split the text
%Since we want to find the words we split using space
txt = textscan(fid,'%s',' ')
%Getting rid of punctuation
for i = 1:len(txt)
word = txt(i)
if word(0) == ',' | word(0) == '.'
txt(i) = word[2:end]
if word(end) == ',' | word(end) == '.'
txt(i) = word[1:end-1]
end
% The unique function can be used to return only an array of the unique elements
wordList = unique(txt)
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.