Take the 2-minute tour ×

how to pick values from matrix closest to or equal to K = 0.5? I know I could obtain values from the matrix, by taking the absolute values and its min. However, I want to be able to loop through the matrix, check if the the first element is equal K, if it is equal,take its index and break. But if the first element is not equal to K, loop until you find value equal to K. Continue until all values equal to K is exhausted. Can anybody point me in the right direction? Thanks in advance.

Here is my code:

data = rand(10,2);k =0.5;
indr = find(data(:,1));
cNum = data(1,1); 
if cNum < k
    old_distance = abs(k - cNum);
    old_distance = abs(cNum - k);
Xdata = data(2:end,:);
indeX = find(Xdata(:,1));
for i = 1:size(Xdata,1)
    if Xdata(i,1) < k
        min_Val = abs(k-Xdata(i,1));     
        new_distance = min(min_Val);          
        min_Val = abs(Xdata(i,1) -k);
        new_distance = min(min_Val);         
    if (new_distance < old_distance)
        old_distance = new_distance;        
        cNum = Xdata(i,1);            
cNum_indeX = indr(indeXm);
Y = cNum;
X = indr(cNum_indeX);'
share|improve this question

3 Answers 3

To find the closest value in a vector to a particular value you can do this:

>> data = rand(10, 1)

data =


>> k = 0.5;
>> [~, index] = min(abs(data - k));
>> closestValue = data(index)

closestValue =

share|improve this answer

For loops are rarely the answer in MATLAB. Let's say you want to check if your array elements are within K ± tol, where tol is some tolerance that you've set. You can do that by simple logical indexing.

tol=0.001; %# set your tolerance here

boolIndex=xVector<=K+tol & xVector>=K-tol; %# xVector is your vector

Now boolIndex is just a logical index array of 0's and 1's. It gives a 1 wherever your array element has satisfied this criteria. You can use this directly in indexing your vector for further manipulation. If, for some reason, you need the exact index, you can get them by doing find(boolIndex==1).

share|improve this answer
You can just run find(boolIndex) - no need to check whether a logical variable is 1. Note that this solution won't give the closest value to 0.5 if it's more than tol away from the target. –  Jonas Mar 13 '11 at 19:15

The constraints of the problem aren't clear enough (and I don't have enough points to make this a comment rather than an answer.)

  • Is speed critical here? If so, then you should avoid any sort of explicit loop. It's usually better to use builtin functions unless the matrix is really huge and you want to break when you find something close enough. If it's millions of entries long, I'd break it into chunks of 10000 or so and let MATLAB use the min function on chunks. Or rows. Or columns. Depends on what you want to do.

  • How close is close enough? You demonstrate with a random matrix, but are you expecting something within rounding error of 0.5?

  • Are you aware that [value,index]=min(x) will give the value and index of minimum?

I assume the matrix must be large, otherwise there would be no downside to letting MATLAB do the vectorized:


That's the best I can do for direction without more... direction.

share|improve this answer
yes, I have [value,index]=min(x) in my code. I would like to loop through the whole matrix, closest values to 0.5. –  user587646 Mar 13 '11 at 7:58
Actually, the next solution is better than mine, but I still don't understand what you're trying to do and why these solutions aren't adequate. They seeem to meet the criteria you stated. –  Ricky Mar 13 '11 at 8:06
@user587646: R.M.'s solution is what you're looking for. Logical indexing is much much faster than loops. –  user564376 Mar 13 '11 at 14:57

Your Answer


