4.0

4.0 | 2 ratings Rate this file 9 Downloads (last 30 days) File Size: 1.35 KB File ID: #30029

findNearest algorithm

by Alexander Wallar

 

14 Jan 2011 (Updated 18 Jan 2011)

This algorithm sees if data you are searching for is in x and if not it shows the closest value

| Watch this File

File Information
Description

This is a search algorithm but what makes it different is that it checks if the data you are looking for in the data matrix. if it is, it returns the position of it and the value at that position. if what you are searching for is not in the data matrix, the algorithm will return the closest value to what you are searching and return the value and its position inside the matrix

ex:
>>findNearest([1 2 3 4 5],3.6)
ans =
Nearest Value = 4
Position = 4

Acknowledgement:
MIT opencourseware homework assignment but no code was given just the idea.

MATLAB release MATLAB 7.10 (2010a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (13)
15 Jan 2011 Jan Simon

It would be better to reply the found values as outputs instead of writing it to the command window. Without output, the function does not accomplish anything.
The function fails to find a value nearest to 0, because it checks for SIGN(a)==1 and ==-1 only.
What do you want to achieve by ' ' ' In the line:
"(['Position = ' ' ' num2str(findVec)]);"?

There is absolutely no need to handle different cases. Shorter and much more efficient:
  function [value, index] = findNearest(desiredVal, x)
  [dummy, index] = min(abs(desiredVal - x(:)));
  value = x(index);

16 Jan 2011 Thomas Benson

There is already a submission to the file exchange from 2002 called find nearest.m which does the same thing, but in addition it has proper outputs and an optional 'bias' input for finding nearest values less than or greater than the test value. http://www.mathworks.com/matlabcentral/fileexchange/2838-findnearest-m

16 Jan 2011 Alexander Wallar

@Thomas Benson

I just saw the other findNearest function and the description said that it works slowly. I can assure you that mine works fast. Use tic toc to check. And i have just submitted a new findNearest function that will return outputs rather than displaying them as strings. Im sorry for the inconvenience I am new to MATLAB and i did not know how to return values.

Thank You
 

16 Jan 2011 Alexander Wallar

@ thomas benson

i have just submitted a program that also finds the index and returns them not displays them. Thank you for looking over my work.

alex wallar

16 Jan 2011 Alexander Wallar

To everybody:

I have submitted two new improvements to this algorithm that should be online by tomorrow. Please use the newest version because this one will RETURN the index and values closest to the ones you are searching for. These improvements will also tell you the closest value to zero but i accidentally forgot to return the index. You can do this simply by changing the 27th line to nearVal = [min(abs(x)) find(min(abs(x)))];

Thank You

17 Jan 2011 Jos (10584)

See
http://www.mathworks.com/matlabcentral/fileexchange/8939-nearestpoint

for another fast and vectorized solution with proper outputs that allows for multiple desired values.

17 Jan 2011 Alexander Wallar

I have told everybody that ny fast vectorized improvements will be online once they get checked. If yall could just wait yall will see a nice fast algorithm. I say again i did not nlknow how to return values and know i do. The new algorithms return the values. Please download the new algorithm as soon as it goes online.

Thank yall
Alex wallar

17 Jan 2011 Alexander Wallar

To everybody,

The updates have just been submitted for findNearest and should be up shortly. Thank you for your wait and comments.

Alexander Wallar
 

18 Jan 2011 Jan Simon

@Alexander: I cannot see "fast vectorized improvements". Your implementation is still slow, because the comparisons are calculated repeatedly. Example:
  if x(x==desiredVal) == desiredVal
    findVec = x==desiredVal;
    nearVal = [x(findvec) find(findVec==max(findVec))]
  else ...
The vector [x==desiredVal] is calculated twice! This wastes time and memory. Or the part for searching for 0:
  nearVal = [min(abs(x)) find(x==min(abs(x)))];
Obtaining "min(abs(x))" twice is a waste of time. Read "help min" to learn how to use the 2nd output of MIN.
As I've written above, there is no reason to split the processing for the SIGN of the searched value. This increases the confusion level only, because the algorithm does not depend on the SIGN at all.
The above posted alternative needs less than the half time: function [value, index] = findNearest(desiredVal, x)
  [dummy, index] = min(abs(desiredVal - x(:)));
  value = x(index);
The results of your function are sometimes strange:
  findNearest(zeros(1,5), 0)
  >> [0,0,0,0,0,1,2,3,4,5]
It is not useful to reply the value and the index in a [1x2] vector, better use two outputs.
Why do you check for "nargin==1", if you actually want an error for "nargin~=2"?
My rating: not useful for processing or education, limited usability and efficiency. Help text is not sufficient and does not follow the Matlab conventions. Outputs for repeated matchs are either wrong or at least not explained in the help.
I'm really astonished that you publish a function before you have learned how values are returned from functions. Bold.

18 Jan 2011 Alexander Wallar

@Jan 1 2 3 4 5 are the indices of of where the nearest values are. Im sorry for publishing.

18 Jan 2011 Alexander Wallar

if anybody who is reading this could just overlook the comment that was written by Jan Simon because he is 100% correct about the old findNearest function but thanks to him i have changed most of the code and have submitted it just 30 minutes ago. The changes should be online shortly. I can assure everybody that my function is good and will work more efficiently than before. I am using multiple outputs and have integrated everything that Jan Simon has told me to correct. There are no more bugs, well at least i hope not.

Thank you very much for downloading,

Alexander Wallar

18 Jan 2011 Alexander Wallar

The new algorithm is in. Please overlook the poor rating because what Jan Simon has rated is a totally different function. Please Download!!!!

Alexander Wallar

18 Jan 2011 Jan Simon

@Alexander: No, do not overlook my initial poor rating! It is a valuable remark for a submission, which was improved fast and dramatically. It seems like you have learned a lot about Matlab in the last 3 days.

A further idea to increase the speed:
Original:
  if ismember(x,desiredVal) == 1
    index = find(x == desiredVal);
    nearVal = x(x==desiredVal);
Faster:
  index = find(x == desiredVal);
  if ~isempty(index)
    nearVal = x(index);

Now the help section does still not satisfy the Matlab conventions, but it is complete and explains the input and output. The current version works as documented and is therefore useful and usable. Some other similar functions in the FEX are more powerful, so I actually tend to a 3 star rating. But I'm really impressed by the rapid progress of this submission. Thanks!

Please login to add a comment or rating.
Updates
17 Jan 2011

ALL THE IMPROVEMENTS EVERYBODY WANTS

18 Jan 2011

more improvements

Tag Activity for this File
Tag Applied By Date/Time
search Alexander Wallar 14 Jan 2011 17:33:01
findnearest Alexander Wallar 14 Jan 2011 17:33:01
data Alexander Wallar 14 Jan 2011 17:33:01

Contact us at files@mathworks.com