Write a MATLAB Function myfun that Takes as input a vector o

Write a MATLAB Function my_fun that: Takes as input a vector of numeric values vals and a key value key as its only two inputs Determines how many occurrence of key exist in the vector vals Seta the functions output variable to (returns) this number of occurrences.

Solution

Matlab Code:

function n = my_fun(y,k)
[r c]=size(y);
n=0;
for i=1:c
if y(i)==k%checks if asked key is equal to a value in given vector
n = n+1;
end
end
end

Output:

>> my_fun([2 5 3 7 5],5)

ans =

2

>> my_fun([2 5 3 7 5],7)

ans =

1

>> my_fun([2 5 3 7 5],1)

ans =

0

 Write a MATLAB Function my_fun that: Takes as input a vector of numeric values vals and a key value key as its only two inputs Determines how many occurrence o

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site