How can I simplify this For loop?

1 次查看(过去 30 天)
Kenneth Wong
Kenneth Wong 2015-11-4
回答: Noam 2015-11-4
Hello. I am facing a problem running this code to completion as the size of T is very big (over a million rows). While the time taken to compute one iteration isn't very long (0.2118s); assuming 1 million data points, the code will still take 100,000s. Thank you for your help!
function Xout = selectpoints(T,Xin)
for k = 1 : length(T)
dist = (Xin(:,1)-T(k,1)).^2 + (Xin(:,2)-T(k,2)).^2 + (Xin(:,3)-T(k,3)).^2;
[~,ind] = min(dist);
nearest(k) = ind;
end
Xout = Xin(nearest,:);

回答(1 个)

Noam
Noam 2015-11-4
pdist2 will do the work

类别

Help CenterFile Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by