3. (10 points) Write a function called myfind that will search for a key in a ve
ID: 3931391 • Letter: 3
Question
3. (10 points) Write a function called myfind that will search for a key in a vector and return the indices of all occurrences of the key, like the built-in find function. It will receive two arguments the vector and the key-and will return a vector of indices or the empty vector [] if the key is not found The function will be invoked as follows: lax my find (vec, key); Following is an example of correct function behavior: >> vec - randi( [5, 10], 1, 10) vec = 5 >> key = 10; >> idx = my find (vec, key) ldx - 2 >> key = 2; >> idx = my find (vec, key) 10 10 7 5 7 10 10 3 10 Empty matrix: 1-by-0 Do not use the in-built find function.Explanation / Answer
A function is a group of statements and it specified for particular task to perform. In this function myfind is the name of the function. And save it as a myfind.m
If the user enter wrong input throws an error message.
myfind.m:
function idx = myfind(vec, key);
if ~isvector(vec)
error('Input must be a vector')
end
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.