Get the inbetween values or the closest value

1 次查看(过去 30 天)
Hi,
I have a matrix a that contains two columns; 1st column is starting point and second column is ending. I have a vector b and for each element of vector b i want to get the starting and ending point it belongs to.
let's say for b=11321.56 , i should get a= 11320.17569 - 15096.09968 as this value comes in between these values
I tried to get the closest value with following code
format long
a=load('data.csv');
b= [11321.56; 15096.23; 19321.76; 65298.26; 94645.23];
for i =1:length(b)
v = b(i);
[~,closestIndex] = min(abs(a-v));
index1(i) = min(closestIndex);
end
The answer should be
index1 = 4 5 6 18 25
But i am getting the wrong answer. What can i do more?
P.S: Data is attached. Also this is just the small data

采纳的回答

Star Strider
Star Strider 2019-5-27
The find function is perfect for this:
for i =1:length(b)
closestIndex(i) = find((b(i) >= a(:,1)) & (b(i) < a(:,2)));
end
closestIndex =
4 5 6 18 25

更多回答(0 个)

类别

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

产品

Community Treasure Hunt

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

Start Hunting!

Translated by