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
댓글 수: 0
답변 (2개)
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
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!