Question 1 Write a function catit that will receive one input argument which is
ID: 3797009 • Letter: Q
Question
Question 1
Write a function catit that will receive one input argument which is a cell array. If the cell array contains only strings, it will return one string which is all of the strings from the cell array concatenated together-otherwise, it will return an empty string. Here is one example of calling the function:
>> fishes = {'tuna', 'shark', 'salmon', 'cod'};
>> catit(fishes)
ans=
tunasharksalmoncod
Question 2
Vectorize this code! Write one assignment statement that will accomplish exactly the same thing as the given code (assume that the variable vec is a vector and it has been initialized):
newv = zeros(size(vec));
myprod = 1;
for i = 1:length(vec)
myprod = myprod * vec(i);
newv(i) = myprod;
end
newv % Note: this is just to display the value
Question 3
Write a function function named:posnum that prompts the user to enter a positive number and loops to error-check until user enters positive number. It returns the positive number entered by the user and print it out on the screen. It calls a subfunction in the loop to print an error message. The subfunction has a persistent variable to count the number of times an error has occurred. [10]
Here is the sample output of calling the function: posnum
>> enteredvalue = posnum
Enter a positive number: -5
Error # 1 : Follow instructions!
Does -5.00 look like a positive number to you?
Enter a positive number: -33
Error # 2 . Follow instructions!
Does -33.00 look like a positive number to you?
Enter a positive number: 6
enteredvalue =
6
Question 4
Use MATLAB built-in function rand to generate 10 random integers between 21 to 150 using and store those integers in a vector v. If the integers are distributed evenly in this range, they should fall on a straight line when arranged from lowest to highest. Assume the sorted integers are stored in a vector y with corresponding index vector x, use the
MATLAB to fit the integers into a straight line.
Explanation / Answer
Ans 1)
function concatString = catit(C)
%to check cell array has only string
if (iscellstr(C))
str = strtrim(strjoin(C));
%to remove any space
result = regexprep(str, 's' , '')
end;
end;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.