Error on Matlab coder

function [nearest]=kdtree_search(data,K)
numDP=size(data.X,1);
P=data.X;
DIM=size(data.X,2);
nearest=zeros(numDP,K);
nsorted=cell(numDP,1);
distance=cell(numDP,DIM);
F=cell(numDP,DIM);
for i=1:numDP
for jg=2:numDP
F{jg}=(P(jg,:)-(P(i,:)));
distance{i}=sqrt(sum(((F{jg}).^2),2)); %THIS LINE
[~,nsorted{i}]=sortrows(distance{i},'ascend');
nearest(i,:)=nsorted{i}(2:K+1)';
While using this code I always get arror in the line that i highlighted bold. It gives size match errror during calculation but i want to do this code faster as it can possible.

6 comentarios

Walter Roberson
Walter Roberson el 4 de Feb. de 2020
Please post the complete error message.
Zeynep Naz Kocabas
Zeynep Naz Kocabas el 4 de Feb. de 2020
Ekran Alıntısı.PNG
Walter Roberson
Walter Roberson el 4 de Feb. de 2020
The error is correct: F{1} is never assigned to.
I notice by the way, that you appear to be using a symmetric distance measurement. That would suggest that you should be using
for i=1:numDP-1
for jg=i+1:numDP
As you are not saving the F values outside of the for jg loop (you would need a 2D cell array for that), I wonder if you even need to save them at all? Are you accessing the F entries after the for jg loop?
I also see
distance=cell(numDP,DIM);
but you are assigning to distance{i} not to a 2D location, and you are overwriting distance{i} every iteration of your for jg loop.
Zeynep Naz Kocabas
Zeynep Naz Kocabas el 5 de Feb. de 2020
Should I not use
distance=cell(numDP,DIM);
Yes i don't use F after the loop.
And thank you for your help.
Walter Roberson
Walter Roberson el 6 de Feb. de 2020
function [nearest]=kdtree_search(data,K)
numDP = size(data.X,1);
P = data.X;
nearest = zeros(numDP,K);
distance = zeros(numDP,numDP);
for i=1:numDP
for jg=i+1:numDP
F=(P(jg,:)-(P(i,:)));
distance(i,jg) = sqrt(sum(((F).^2),2));
distance(jg,i) = distance(i,jg); %symmetric
[~,nsorted]=sortrows(distance{i},'ascend');
nearest(i,:)=nsorted(2:K+1)';
end
end
Zeynep Naz Kocabas
Zeynep Naz Kocabas el 9 de Feb. de 2020
Thank you very much.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Etiquetas

Preguntada:

el 4 de Feb. de 2020

Comentada:

el 9 de Feb. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by