dsearchn() Command is slowing down my algorithm, Any better Solution? MATLAB

I am using the following code to calculate altitude.
Data = [Distance1',Gradient];
Result = Data(dsearchn(Data(:,1), Distance2), 2);
Altitude = -cumtrapz(Distance2, Result)/1000;
Distance 1 and Distance 2 has different size with same values so I am comparing them to get corresponding value of Gradient to use with Distance 2.
Just to execute these 3 lines the Matlab takes 12 to 15 seconds. Which slow down my whole algorithm.
Is there any better way I can perform above action without slowing down my algorithm?

댓글 수: 11

Yeah of course, almost anything else is better, why on earth are you using DSEARCHN for numbers? Use INTERP1 with 'nearest' option.
Can you please elaborate with an example in my case how should i use it?
Bruno Luong
Bruno Luong 2018년 10월 26일
편집: Bruno Luong 2018년 10월 26일
Other way around: first you give us a (small) example.
Shahab Khan
Shahab Khan 2018년 10월 26일
편집: Shahab Khan 2018년 10월 26일
I believe I already have given an example in my question about what i am trying to do.
You give a piece of code, without telling the size the class of your Data. No one can run your code if copy/past in MATLAB. Almost useless for people who tries to answer the question.
An "example" is some extra instruction to generate a small piece data, (and eventually an expecting output if the code is buggy and does not produce what you expect).
Apologies.
Here is my example.
Distance2= [1:10:1000]';
Distance1= [1:1:1000]';
Gradient= rand(1000,1);
Data= [Distance1,Gradient];
Result = Data(dsearchn(Data(:,1), Distance2), 2);
Replace
dsearchn(Data(:,1), Distance2)
by
interp1(Distance1,Distance2,'nearest');
This gives me distance values, I actually required gradient values from second column of Data corresponding to Distance1 in Data compared to Distance2 values.
For example, If Distance2 value is equal to Distance1 value in Data choose the corresponding value from second column in Data.
This command worked actually.
Result = interp1(Data(:,2),Distance2,'nearest');
Thank you for your help
I wrote replace just the DSEARCH, not the rest.
If you want to get the same Result as with
Data = [Distance1',Gradient];
Result = Data(dsearchn(Data(:,1), Distance2), 2);
and Data is just temporary variable and you don' mind to trash it away after, then
Result = interp1(Distance1,Gradient,Distance2,'nearest');

댓글을 달려면 로그인하십시오.

 채택된 답변

Bruno Luong
Bruno Luong 2018년 10월 27일
편집: Bruno Luong 2018년 10월 27일
Replace
dsearchn(Data(:,1), Distance2)
by
interp1(Distance1,Distance2,'nearest');
If you want to get the same Result as with
Data = [Distance1',Gradient];
Result = Data(dsearchn(Data(:,1), Distance2), 2);
and Data is just temporary variable and you don' mind to trash it away after, then
Result = interp1(Distance1,Gradient,Distance2,'nearest');

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Type Identification에 대해 자세히 알아보기

제품

릴리스

R2018a

질문:

2018년 10월 26일

편집:

2018년 10월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by