Get vector position for specific values

4 vues (au cours des 30 derniers jours)
Luis Lopez
Luis Lopez le 19 Août 2016
Commenté : Luis Lopez le 19 Août 2016
Hi,
I have two vectors, one of them has a large data values, the other has some values that I want to find in the first one to get the position.
Trip_dst;%%This vector has the complete data
Cumm_Trip_Dist;%%%This vector has specific values that I want to find in "Trip_dst" vector
%%I'm trying to use this to find the first value, which difference is not more than 0,05
Cumm_Trip_DistLength = length(Cumm_Trip_Dist);%%Get the lenght for the data required
for AP = 1:Cumm_Trip_DistLength
AC = find(Trip_dst < (Cumm_Trip_Dist(AP)+.05)& Trip_dst > Cumm_Trip_Dist(AP));
AB(AP,1) = AC(1);%%Get the first value(the nearest), AB is my vector with the positions
end
However as not all data that I'm looking for from the second vector is in the first one I got the next message:
"Attempted to access AC(1); index out of bounds because numel(AC)=0".
What can I do to just skip this value not founded and try to find the next one?
Hope you can help me, maybe this has a really easy solution.
Regards

Réponses (2)

Azzi Abdelmalek
Azzi Abdelmalek le 19 Août 2016
Modifié(e) : Azzi Abdelmalek le 19 Août 2016
for AP = 1:Cumm_Trip_DistLength
AC = find(Trip_dst < (Cumm_Trip_Dist(AP)+.05)& Trip_dst > Cumm_Trip_Dist(AP));
if ~isempty(AC)
AB(AP,1) = AC(1);%%Get the first value(the nearest), AB is my vector with the positions
else
AB(AP,1)=nan
end
end
%If you want to find the first value that meets the criterion, look at this example
A=[1 2 3 2 4 5 2]
idx=find(A==2,1)
  1 commentaire
Luis Lopez
Luis Lopez le 19 Août 2016
Great, it Works!!

Connectez-vous pour commenter.


Star Strider
Star Strider le 19 Août 2016
I don’t have your data, so I’m guessing here with created data.
See if this does what you want:
Cumm_TripDist = randi(20, 1, 10);
TripDist = randi(99, 1, 100);
for k1 = 1:length(Cumm_TripDist)
[~,AB(k1)] = ismembertol(Cumm_TripDist(k1), TripDist, 0.05);
end

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by