Main Content


Manhattan distance weight function



Z = mandist(W,P) takes an S-by-R weight matrix, W, and an R-by-Q matrix of Q input (column) vectors, P, and returns the S-by-Q matrix of vector distances, Z.

mandist is the Manhattan distance weight function. Weight functions apply weights to an input to get weighted inputs.

mandist is also a layer distance function, which can be used to find the distances between neurons in a layer.


D = mandist(pos) takes the N-by-S matrix of neuron positions, pos, and returns the S-by-S matrix of distances, D.


collapse all

This example shows how to calculate the weighted input matrix.

Define a random weight matrix W and input vector P and calculate the corresponding weighted input Z.

W = rand(4,3);
P = rand(3,1);
Z = mandist(W,P)

This example shows how to calculate the distances of 10 neurons arranged in a three-dimensional space.

Define a random matrix of positions for 10 neurons arranged in three-dimensional space and then find their distances.

pos = rand(3,10);
D = mandist(pos)

Input Arguments

collapse all

Weight matrix, specified as an S-by-R matrix.

Input matrix, specified as an R-by-Q matrix of Q input (column) vectors.

Matrix of neuron positions, specified as an N-by-S matrix.

Output Arguments

collapse all

Matrix of vector distances, returned as an S-by-Q matrix.

Matrix of distances, returned as an S-by-S matrix.

More About

collapse all

Network Use

To change a network so an input weight uses mandist, set net.inputWeights{i,j}.weightFcn to 'mandist'. For a layer weight, set net.layerWeights{i,j}.weightFcn to 'mandist'.

To change a network so a layer’s topology uses mandist, set net.layers{i}.distanceFcn to 'mandist'.

In either case, call sim to simulate the network with dist. See newpnn or newgrnn for simulation examples.


The Manhattan distance D between two vectors X and Y is

D = sum(abs(x-y))

Version History

Introduced before R2006a

See Also

| |