Question

I got a data of safety factor and its corresponding force, I wan to make it like prompting and asking ''what is the safety factor?' then the user will enter the value of safety factor and I wan an output displays the corresponding force in the data table

thx for any help.

Was it helpful?

Solution

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);

OTHER TIPS

I think you are simply wanting to index the value. Matlab indexes start at 1, so you either need to use that as your reference, or add one. The correct was is to put the desired index in parenthesis, as so

output_value=A(y+1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top