Since you want to search a value in first column and print the corresponding value from second column you can do something like this:
ri = find(A(:,1) == y);
z = A(ri,2);
This assumes that you will always find the corresponding value of y
in first column of A
. If that is not the case, you need to modify the code:
ri = find(A(:,1) == y);
if isempty(ri) % value not found
% code to print error message
% or
% code interpolate between given values
else
z = A(ri,2);
end
you can also use try-catch
block instead of if-else
statement.
Interpolation can be bit trick as you need to model it correctly. It is still much easier to interpolate than extrapolate.
Assuming your the values in first column are arranged in increasing or decreasing order, you can use interp1
as follows, for first order interpolation,
z = interp1( A(:,1), A(:,2) , y);