Closest value(s) from a list

Returns the closest value from a list for each target value
291 Downloads
Updated 22 Apr 2011

View License

Returns the closest value from a list

Syntax

c = closest(a,b); returns the closest c value in a for each value in b
[c,i] = closest(a,b); returns the index i of the closest value in a for each value in b
[c,i,d] = closest(a,b); returns the difference d of the closest value in a for each value in b

Example

[cvalue, cindex cdiff] = closest([-8 2 5.5 12 45 20 100],[-15 2.3 10 50])

cvalue =

-8 2 12 45


cindex =

1 2 4 5


cdiff =

7.0000 0.3000 2.0000 5.0000

Cite As

Javier Lopez-Calderon (2024). Closest value(s) from a list (https://www.mathworks.com/matlabcentral/fileexchange/31145-closest-value-s-from-a-list), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2010a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Sparse Matrices in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.0.0