Get the inbetween values or the closest value

조회 수: 3 (최근 30일)
ARN
ARN 2019년 5월 27일
답변: Star Strider 2019년 5월 27일
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