Effacer les filtres
Effacer les filtres

Get the inbetween values or the closest value

1 vue (au cours des 30 derniers jours)
ARN
ARN le 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

Réponse acceptée

Star Strider
Star Strider le 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

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by