I have an Nx2
matrix of data points where each row is a data point. I also have an NxK
matrix of indices of the K
nearest neighbours from the knnsearch
function. I am trying to create a matrix that contains in each row the data point followed by the K neighbouring data points, i.e. for K = 2
we would have something like [data1, neighbour1, neighbour2]
for each row.
I have been messing round with loops and attempting to index with matrices but to no avail, the fact that each datapoint is 1x2
is confusing me.
My ultimate aim is to calculate gradients to train an RBF network in a similar manner to:
D = (x_dist - y_dist)./(y_dist+(y_dist==0));
temp = y';
neg_gradient = -2.*sum(kron(D, ones(1,2)) .* ...
(repmat(y, 1, ndata) - repmat((temp(:))', ndata, 1)), 1);
neg_gradient = (reshape(neg_gradient, net.nout, ndata))';