Why knnsearch () function slows down the code?
1 次查看(过去 30 天)
显示 更早的评论
I have to make feature vector in which I have to store distance between a candidate feature point and its four neighboring feature points. I am using knnsearch() for this purpose. However it slows down the code. How can I improve this?
Below is my code.
N = sum(cn_image(:) == 1) + sum(cn_image(:) == 3) + sum(cn_image(:) == 4); %Number of rows in feature Matrix
featr_vect = zeros(N,8);
for i = 1:size(cn_image,1)
for j = 1 : size(cn_image,2)
if (cn_image(i,j) == 1) || (cn_image(i,j) == 3) || (cn_image(i,j) == 4)
[Idx D] = knnsearch(cn_image(:), cn_image(i,j), 'k', 4, 'distance, 'euclidean');
end
end
end
2 个评论
回答(1 个)
Jan
2017-4-3
编辑:Jan
2017-4-3
Currently your code overwrites Idx and D in each iteration. This is a massive waste of time. If only the last classification is wanted:
index = find(ismember(cn_image(:), [1, 3, 4]), 1, 'last');
[Idx D] = knnsearch(cn_image(:), cn_image(index), 'k', 4, 'distance, 'euclidean');
If the overwriting of Idx and D appears in the code posted here only and not in the real code: Please post the relevant part of the code. Such abbreviations are misleading frequently.
Optimizing code is hard, when the readers cannot run it. Better post some relevant input data, such that we can check our suggestions.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Classification Trees 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!