Get the inbetween values or the closest value
3 次查看(过去 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
0 个评论
采纳的回答
Star Strider
2019-5-27
for i =1:length(b)
closestIndex(i) = find((b(i) >= a(:,1)) & (b(i) < a(:,2)));
end
closestIndex =
4 5 6 18 25
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!