How to get a value from a matrix?

4 次查看(过去 30 天)
Hi guys, I have a question that on the first glance seems really simple. I have a matrix of 2 columns and I have a value of the first column and want to know the corresponding value from the second column, For instance I have the following matrix:
Data =
0.1 0.50
0.15 0.72
0.2 0.37
0.25 0.18
0.3 0.65
I have the first value, 0.25 and now i want to know how to get the 0.18 from the matrix. The matrix of my problem has much more rows, but it's the same principle. I already written a code that works when I have 100 rows, but when I have 200 rows it doesn't work anymore (data of the second and first column also depend on the number of rows). This is the code I already wrote:
Uva = [x; T_plot]' %This is my matrix
ind1 = Uva(:,1) == 0.591; %Look for the right x
A1 = Uva(ind1,:); %Pick the row
Tsim = A1(2); %Assign the outcome to a new name
Can someone please help me?

采纳的回答

Stephen23
Stephen23 2019-1-12
编辑:Stephen23 2019-1-12
You should never compare for exact equivalence of floating point numbers, read this to know why:
Always compare the absolute difference of values against a tolerance, like this:
>> data = [0.1,0.50;0.15,0.72;0.2,0.37;0.25,0.18;0.3,0.65]
data =
0.10000 0.50000
0.15000 0.72000
0.20000 0.37000
0.25000 0.18000
0.30000 0.65000
>> val = 0.15;
>> idx = abs(data(:,1)-val)<1e-5;
>> out = data(idx,2)
out = 0.72000
  1 个评论
Danny Helwegen
Danny Helwegen 2019-1-12
Thanks for your help, to take a tolerance in account is a really good advice

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by