Help with knnsearch searching through mirrored ghost particles.

I've attached the necessary workspace to recreate what I have tried to do. Essentially, I am trying to print the particle ID's and distances of their nearest neighbors ( up to 20 right now, that's what 'res' is), with the search INCLUDING ghost particles 4001-8000.
There are 4000 particles and 4000 mirrors, and the position (x,y) values for those 1-4000 are written to 'Y' ; then those values are mirrored over the x axis to create axi-symmetric tracers for a proto neutron star, and those mirror particles are written to 'X' (ID # 4001-8000).
I thought that by using 'full' - the concatenated group of all 8000 particles - in the knnsearch call that it would do what I want it to do: search across the pole (x axis) for neighbors as well, not just along the rays the particles belong to. Along one ray, the particle ID's will be multiples of 40, i.e. 1, 41, 81, 121, ... etc are in the same ray.
However, it's not, it simply moves along the ray and then eventually jumps to the rays beside of it. I'm going to include the output IDX before and separate from the code, which is the output for the ID's for all particles up to their 20th closest neighbor, so you can see which particles it chooses. Just for clarity, though, I don't want the neighbor ID's of all 8000. Just the 4000 regular particles, because if it works correctly, the neighbors should be the same for mirrors, i.e. particle 1 and particle 4001 should have the same neighbors because they are mirrors of each other.
val(:,:,1) =
Columns 1 through 18
1 41 81 121 161 201 241 281 321 361 401 441 481 521 561 2 42 601
Columns 19 through 20
82 122
val(:,:,2) =
Columns 1 through 18
1 41 81 121 161 201 241 281 321 361 401 441 481 521 561 2 42 82
...
val(:,:,3999) =
Columns 1 through 9
3680 3720 3640 3760 3600 3800 3560 3840 3520
Columns 10 through 18
3880 3480 3440 3920 3400 3960 3360 3320 4000
Columns 19 through 20
3280 3240
val(:,:,4000) =
Columns 1 through 9
3840 3880 3800 3920 3760 3960 3720 3680 4000
Columns 10 through 18
3640 3600 3560 3520 3480 3440 3400 3360 3320
Columns 19 through 20
3280 3240
--------------------------------------------------------------------------------------------------------
function [IDX, D] = neighborsearch(MdlKD,X,Y, k, res)
%The IDX and Distance matrices are initialized
IDX (1, 1:res, 1:4000) = 0;
D (1, 1:res, 1:4000) = 0;
full = [Y;X];
% For-loop through REGULAR particles (1-4000) and
%outputs distances and ID's of neighbors %
for i=1:size(Y, 1)
[IDX2, D2] = knnsearch(MdlKD,full(i,:),'k', res); %Selecting neighbors from all particles,
% Including ghosts (4001-8000)
IDX(:,:,i) = IDX2; % Adding to the existing matrices
D(:,:,i) = D2;
%display the loop iteration number and time %
fprintf('\tLoop: %4d at %4d-%02d-%02d %02d:%02d:%06.3f\n', i, datevec(now))
end
end

Answers (0)

Asked:

on 20 Jul 2015

Edited:

on 20 Jul 2015

Community Treasure Hunt

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

Start Hunting!