-
Notifications
You must be signed in to change notification settings - Fork 1
/
M2S_findNeighbours.m
27 lines (22 loc) · 1.23 KB
/
M2S_findNeighbours.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
% This function is to be used with M2S_calculateResiduals
function [MatchSet_neighbors_idx]=M2S_findNeighbours(MatchSet,MatchSet_idx,Set,Set_idx,nrNeighbors,XdimForNeighbors)
MatchSet_neighbors_idx=NaN(size(Set,1),nrNeighbors);
for featurePair = 1:size(Set,1)
current_Set_idx = Set_idx(featurePair);% current idx initial
temp_MatchSet=MatchSet;
temp_MatchSet(MatchSet_idx==current_Set_idx,:)=NaN;
% calculate zscores
%[Z_MatchSet,MU,SIGMA] = zscore(temp_MatchSet(:,Xdim));
MU = nanmean(temp_MatchSet(:,XdimForNeighbors));
SIGMA = nanstd(temp_MatchSet(:,XdimForNeighbors));
Z_MatchSet = (temp_MatchSet(:,XdimForNeighbors) - MU)./ SIGMA;
Z_Set_featurePair =( Set(featurePair,XdimForNeighbors)- MU ) ./ SIGMA ;
% Find neighbours indices:
% Using machine learning toolbox
% MatchSet_neighbors_idx(featurePair,:) = knnsearch(Z_MatchSet,Z_Set_featurePair,'K',nrNeighbors);
% Not using machine learning toolbox
tempDist = Z_MatchSet - repmat(Z_Set_featurePair,size(Z_MatchSet,1),1);
tempDist_sqrtSSQ = sqrt(sum(tempDist.*tempDist,2));
[~,sorted_tempDist_sqrtSSQ_idx]= sort(tempDist_sqrtSSQ);
MatchSet_neighbors_idx(featurePair,:) = (sorted_tempDist_sqrtSSQ_idx(1:nrNeighbors))';
end