How to get a value from a matrix?
3 views (last 30 days)
Show older comments
Danny Helwegen
on 12 Jan 2019
Commented: Danny Helwegen
on 12 Jan 2019
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?
0 Comments
Accepted Answer
Stephen23
on 12 Jan 2019
Edited: Stephen23
on 12 Jan 2019
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
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!