Find lower query points compared to the Reference vector

Hi all, I have two vectors, one is "Beg_Data" which has 19 values and I have another reference vector "Beg_In_Ex" which contains 1239 values (Data attached). I am computing a new vector "End_Cycle" same size (19 values) that of "Beg_Data" vector based on the nearest neighbour search. I would like to achieve the results of "End_Cycle" values lower than that of corresponding values that of "Beg_Data". However, using the method that I have attempted I found some values are always higher than that of the "Beg_Data" values. As you can see I want the difference to be always positive (End_Cycle<Beg_Data). Any help in this regard is highly appreciated.
load data.mat;
End_Cycle = dsearchn(Beg_In_Ex',Beg_Data);
End_Cycle = Beg_In_Ex(End_Cycle)';
Start_Stop = [End_Cycle Beg_Data]
Start_Stop = 19×2
1.0e+06 * 0.0746 0.0761 0.2064 0.2047 0.2327 0.2346 0.4144 0.4130 0.5276 0.5263 0.6324 0.6329 0.8096 0.8088 0.9534 0.9518 0.9722 0.9702 0.9777 0.9760
Difference = Start_Stop(:,2)-Start_Stop(:,1)
Difference = 19×1
1.0e+03 * 1.4242 -1.6832 1.9540 -1.3347 -1.3141 0.4402 -0.7642 -1.6077 -2.0212 -1.6784

3 件のコメント

Ganesh Naik
Ganesh Naik 2022 年 8 月 7 日
Dear Voss, thanks for the answer, this is what i wanted. This method clearly gives me the nearest point from the left side of the reference point. I am going to accept this answer.
I have another question, for instance, If I want the differences always negative (I mean, getting the nearest point on the right hand side), is it possible to modify this code slightly and achieve the results? Please let me know your opinion.
Kind regards
Voss
Voss 2022 年 8 月 7 日
Just change the definition of idx(ii):
idx(ii) = find(Beg_In_Ex >= Beg_Data(ii),1);
Ganesh Naik
Ganesh Naik 2022 年 8 月 8 日
Dear Voss, thanks, this is perfect and it works.
Kind regards

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

 採用された回答

Voss
Voss 2022 年 8 月 6 日

0 投票

load data.mat;
idx = zeros(size(Beg_Data));
for ii = 1:numel(Beg_Data)
idx(ii) = find(Beg_In_Ex <= Beg_Data(ii),1,'last');
end
End_Cycle = Beg_In_Ex(idx).';
Start_Stop = [End_Cycle Beg_Data]
Start_Stop = 19×2
1.0e+06 * 0.0746 0.0761 0.2016 0.2047 0.2327 0.2346 0.4108 0.4130 0.5245 0.5263 0.6324 0.6329 0.8071 0.8088 0.9495 0.9518 0.9676 0.9702 0.9732 0.9760
Difference = Start_Stop(:,2)-Start_Stop(:,1)
Difference = 19×1
1.0e+03 * 1.4242 3.0948 1.9540 2.2663 1.7939 0.4402 1.6808 2.2943 2.5998 2.8346

その他の回答 (0 件)

製品

リリース

R2021a

質問済み:

2022 年 8 月 6 日

コメント済み:

2022 年 8 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by