Filter löschen
Filter löschen

Get the inbetween values or the closest value

1 Ansicht (letzte 30 Tage)
ARN
ARN am 27 Mai 2019
Beantwortet: Star Strider am 27 Mai 2019
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

Akzeptierte Antwort

Star Strider
Star Strider am 27 Mai 2019
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

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by