Code covered by the BSD License  

Highlights from
Find values in a vector or function

3.0

3.0 | 1 rating Rate this file 10 Downloads (last 30 days) File Size: 1.69 KB File ID: #31103
image thumbnail

Find values in a vector or function

by

 

19 Apr 2011 (Updated )

For a function y=f(x), this script looks for all the "x" values for a desired value of "y" (y0).

| Watch this File

File Information
Description

Given a desired value "y0", this function finds all the elements "y" in a vector whose values are the closest to "y0" in the whole vector, and also the positions "xi". So, the output is a set of points [xi,y0] given in two vectors, one for the values and another one for the positions.

The inputs are:
-Vector.
-Value that you want to find in the vector "y0".

The outputs are:
-The closest values to "y0" in the function (by means the real "y" values).
-The position "xi" of those "y" values.

I hope it is useful for all of you. If you have any suggestion, please, tell me.

MATLAB release MATLAB 7.9 (R2009b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (2)
16 Mar 2013 Adrian Lara-Quintanilla

Thanks, I appreciate your feedback. I have solved that just yielding the closest value of those in the vector.

Anyway, this is a very simple function. I am sure it can be optimized a lot, but it works very well for my purposes and I decided to share it with the people. I do not know how the "experts" in Matlab deals with this, but I need to use this function in almost everything I do... :)

Again, thanks for helping me to improve :)

16 Mar 2013 arnold

If I understand correctly your function can only find anything if there is at least one value <= AND one >= y. This is not necessarily the case for any given function. This is suboptimal. The function should always find at least the nearest value. imho

Updates
21 Apr 2011

The algorithm remains being the same, but I have made some little changes.
- When the function starts, it will ask for the value that you are looking for.
- It draws a plot to help you to understand the values and positions that the function found.

12 Mar 2013

Little optimization

18 Mar 2013

Yields the closest value in case that the wanted value is not in the range of the input vector

Contact us