Get vector position for specific values

4 次查看(过去 30 天)
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

回答(2 个)

Azzi Abdelmalek
Azzi Abdelmalek 2016-8-19
编辑:Azzi Abdelmalek 2016-8-19
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 2016-8-19
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

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by