Filter löschen
Filter löschen

Get vector position for specific values

1 Ansicht (letzte 30 Tage)
Luis Lopez
Luis Lopez am 19 Aug. 2016
Kommentiert: Luis Lopez am 19 Aug. 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

Antworten (2)

Azzi Abdelmalek
Azzi Abdelmalek am 19 Aug. 2016
Bearbeitet: Azzi Abdelmalek am 19 Aug. 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)

Star Strider
Star Strider am 19 Aug. 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

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by