フィルターのクリア

Get vector position for specific values

1 回表示 (過去 30 日間)
Luis Lopez
Luis Lopez 2016 年 8 月 19 日
コメント済み: Luis Lopez 2016 年 8 月 19 日
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)
  1 件のコメント
Luis Lopez
Luis Lopez 2016 年 8 月 19 日
Great, it Works!!

サインインしてコメントする。


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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by