How to get a value from a matrix?

조회 수: 3 (최근 30일)
Danny Helwegen
Danny Helwegen 2019년 1월 12일
댓글: Danny Helwegen 2019년 1월 12일
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