Question

I have declared 2 matrixes like this:

a = [ 1 2;
      11 12];


[m, n] = size(a);

b = a(2,:); 

dist( b , a ); % the first column is not interesting

This works, I get a vector

[ 10.0499 9.0000 ]

However if I want to add a column or a line to my matrix a:

 a = [ 1  2  3 ;
       11 12 13];

then apply the same algorithm, than above, ignoring or not the first column, I get this error:

Error using -
Matrix dimensions must agree

I have no idea why it does not work, can someone explain to me please?

Actually I don't even know how to retrieve the way this euclidian distance is computed, I failed at trying to retrieve those values [ 10.0499 9.0000 ] by hand. The Matlab mathworks manual says he algorithm used is the following:

d = sum((x-y).^2).^0.5

Any help

Was it helpful?

Solution

It does not work because the dist function when called with two arguments works like this:

Z = dist(W,P) takes an SxR weight matrix and RxQ input matrix and
returns the SxQ matrix of distances between W's rows and P's columns. dist(P',P) returns the same result as dist(P).

That is, if you do this:

a = [ 1  2  3 ;
      11 12 13]
b = a(2,:) % Then b = [11 12 13]

...and call:

dist(b, a)

It will try to compute the distance between b's rows (in this case, only a row with three numbers, that is, a 3D point) and a's columns (each column has two numbers, that is, a 2D point). Measuring a distance between them makes no sense.

The reason it worked on your first example was because the matrix was square (2x2). Therefore, you're computing distances between a row (2D) to the other columns (also 2D).

OTHER TIPS

A distance by definition is a single number, not a vector. The fact that you've getting a vector for matrices of the same size already indicates that something is wrong. In particular, it gives you distance between each of the corresponding column vectors to each other. So it doesn't work when your matrices are of different sizes. A distance between matrices is not defined in any one particular way. I don't know of a notion of Euclidean distance between two matrices.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top