MATLAB: Finding nearest number in matrices

matricesnearestno attemptnumber

hi I have 2 matrices A and B.
A=[0 0.375 0.405 0.452 0.500 0.530 0.577 0.623 0.639 0.670 0.701 0.717 0.748 0.779]
B=0:01:end
I want to find nearest number of A to 0.1 and then to 0.2 and then to 0.3 and …
in this case the nearest numbers to 0.1 and 0.2 is 0.but I want a program that find the nearest number to 0.1 and put that number away and then find the nearest number to 0.2 and so on.
can you help me?

Best Answer

  • What's the purpose of B? What is "end"?
    Anyway, using A, try this:
    clc;
    A=[0 0.375 0.405 0.452 0.500 0.530 0.577 0.623 0.639 0.670 0.701 0.717 0.748 0.779]
    for k = 1 : length(A)
    [~, nearestIndex(k)] = min(abs(A - k/10));
    end
    % Display in command window:
    nearestIndex