Are Nearest Neighbour functions supported on Simulink Realtime?

조회 수: 1 (최근 30일)
Sebastian Wolter
Sebastian Wolter 2020년 9월 2일
댓글: Jiangfeng Liu 2021년 4월 17일
Follow Up for: https://de.mathworks.com/matlabcentral/answers/584957-are-point-cloud-functions-supported-on-simulink-realtime?s_tid=mlc_ans_email_view
So I’ve tried using kdtree + knnsearch in Simulink Realtime (Matlab 2018b)
I’ve created a Matlab Function Block inside Simulink and then calling e.g.
kd = KDTreeSearcher([0 1 2;10 10 10]);
idx = knnsearch(kd,[[0,0,0]]);
It leads to: “Cannot call protected member 'stats.coder.searcher.KDTreeSearcher.KDTreeSearcher'."
If I use createns(X) instead of KDTreeSearcher => Function 'createns' not supported for code generation.
Is there any other space partioning trees available, which supports code generation? Like octree?
knnsearch itself seems to be compatible with slrt c code generation. e.g.
idx = knnsearch([0 1 2;10 10 10],[[0,0,0]]);
But I think this will not use any spatial partitioning trees, right? So just the naive implementation for nearest neighbour search?
According to https://de.mathworks.com/help/stats/code-generation-for-nearest-neighbor-searcher.html it seams that you could save a pretrained KDTree and then search it after deploying to a target, but this does not help in my use case (live dynamic point cloud processing).
  댓글 수: 1
Jiangfeng Liu
Jiangfeng Liu 2021년 4월 17일
Hello, i have tried the same thing as you. It works but very slowly. Do you have any idea about that? Thanks!

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

답변 (1개)

Hamid Satarboroujeni
Hamid Satarboroujeni 2020년 9월 3일
편집: Hamid Satarboroujeni 2020년 9월 3일
If you want ot use the knnsearch method of KDTreeSearcher object inside a MATLAB function block, you must follow the code generation workflow:
Simply put,
1- Create the KDTreeSearcher object in MATLAB:
kd = KDTreeSearcher([0 1 2;10 10 10]);
2- Save a code generation version of the object in a MAT-file:
saveLearnerForCoder(kd,'searcherModel')
3- In the MATLAB Function block create an entry point function to load the saved object and call knnsesarch method:
function idx = myknnsearch1(Y) %#codegen
Mdl = loadLearnerForCoder('searcherModel');
idx = knnsearch(Mdl,Y);
end
You should be able to run simulations and generate C/C++ code for this.
As you pointed out, you also have the option of using the knnsearch function, but to make sure the kdtree searcher object is used specify the NSMethod name-value pair:
idx = knnsearch([0 1 2;10 10 10],[[0,0,0]],'NSMethod','kdtree');
I hope this answers your questions.
  댓글 수: 2
Sebastian Wolter
Sebastian Wolter 2020년 9월 4일
Thanks for the explanation.
But this means i cannot build a kdtree on the fly (with new data) inside a MATLAB function block.
It can only prepare a kdtree with datasets known before and then load this.
Hamid Satarboroujeni
Hamid Satarboroujeni 2020년 9월 4일
편집: Hamid Satarboroujeni 2020년 9월 8일
If you use the knnsearch method you cannot build the kdtree on the fly. But if you use the knnsearch function the kdtree will be built on the fly. It generates code for createns and uses it at runtime to create the kdtree.
The code generation capabilities and limitations of the knnsearch function are listed here:

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

카테고리

Help CenterFile Exchange에서 Code Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by