Matlab Question Write a function to return the index of the value that is nearest to a desired value. The function declaration should be: ind = FindNearest(x, desiredVal). x can be a vector or matrix, and desiredVal is the scalar value you want to find. Do not assume that desiredVal exists in x, rather find the value that is closest to desiredVal. If multiple values are the same distance from desiredVal, return all of their indices. Test your function to make sure it works on a few vectors and matrices. Useful functions are abs, min, and find. Hint: You may have some trouble using min when x is a matrix. To convert a matrix A into a vector you can use the expression b = A(:). Then, expression minVal = min(b) will give you the minimum value in A. To find where this minimum occurs in A, do inds = find(A == minVal);.
Matlab Question
Write a function to return the index of the value that is nearest to a desired value. The function declaration should be: ind = FindNearest(x, desiredVal). x can be a vector or matrix, and desiredVal is the scalar value you want to find. Do not assume that desiredVal exists in x, rather find the value that is closest to desiredVal. If multiple values are the same distance from desiredVal, return all of their indices. Test your function to make sure it works on a few
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images