Euclidean distance of adjacent pairs of matrix

So i have an example matrix
A = [10 20 30 45
32 94 21 15
67 33 22 54
13 27 89 73]
and i want to calculate the Euclidean distance between adjacent pairs of matrix A. In other words, each row in the matrix is a feature vector, hence the calculation is row-wise.
Pardon my english but I'm having issue understanding "adjacent pairs". Does it mean row1 with row2, row2 with row3 and row3 with row4 OR row1 with row2, row1 with row3 and row1 with row4 OR row1,row2,row3 and row4 all together?

4 Comments

To know whether you'd want to compare row 1 with row 3, which is NOT adjacent, we'd need to know the context in which you generated these rows representing some measurements. Perhaps you want the distance between all rows but you (or someone) just said adjacent but really meant all possible permutations. What's the larger picture of how you got these, what they represent, and what addition information you want to extract/compute from those feature vectors?
@Image Analyst terribly sorry to inform but it was because i was referring to a paper published which i recently discovered as a poorly paraphrased paper of the original writer. I managed to track down the original paper which gave more explanations other than just "adjacent pairs".
So i will now rephrase. In A above, each row represents a feature vector of an image. Based on A, the euclidean distance, () is calculated between the adjacent pairs, where , where controls the amount of neighbouring feature vector.
So back to my question, if my = 3, and I'm at position i =1 at start (which is the first row), how would adjacent pairs be compared, since ?
As I said in my answer, in order to have an adjacency relationship, you need a total ordering definition on your set (the feature vectors set). Now this is not my domain, but my understanding is that feature vectors can represent any property you choose. So, feature vector A(1) could encode the dominant colours of the image, feature vector A(2) could encode the orientation of lines in the image, and feature vector A(3) could encode whether or not there's a circle in the upper right corner of the image. How you create a total ordering on that, and hence define adjacency, I don't know.
I think you need to dig more into that paper to understand what they're doing.
In graph theory "adjadcent" usually mean topology connected, you don't need necessary to have order defined on the set.

Sign in to comment.

 Accepted Answer

All pairs
If you have the right toolbox, take a look at PDIST2 function. If not
[m,n] = size(A);
D = sqrt(sum((reshape(A, [m,1,n])-reshape(A, [1,m,n])).^2,3))
D(i,j) contains the distance between A(i,:) and A(j,:)
Adjacent pairs
D = sqrt(sum(diff(A,1,1).^2,2))
D(i) contains the distance between A(i,:) and A(i+1,:)
Windowing adjacent pairs
[m,n] = size(A);
Nnum = 2; % N_number
r = (1:m)'+(-Nnum:Nnum);
inan = m+1;
A(inan,:) = NaN;
r(r<1 | r>m) = inan;
B = reshape(A(r,:),[m, 2*Nnum+1, n]);
A(inan,:) = [];
D = sqrt(sum((reshape(A, [m,1,n])-B).^2,3));
D(i,j) = contains the distance between
A(i,:) and A(i+j-Nnum-1,:);
for i=1,...m and j=1,...,2*Nnum+1.
Note: Distance to overflow points contains NaN.

4 Comments

@Bruno Luong could you take a look at my reply to @Image Analyst above to see if there's anything you would like to look into in terms of adjacent pairs?
Edit my code with the new definition of adjadcent. NOTE the original paper has gross typo error on indexing.
@Bruno Luong I don't quite get the "windowing adjacent pair" part. Do you mind explaining a little bit? As in words like row1 with row2 etc, so i could get the idea.
Sorry, I won't explain with "words". To me it's more confusing. Each person is free to undertand with his/her respective personnal view.

Sign in to comment.

More Answers (1)

I'm a bit confused by your question. You define something you want to do then ask us to explain your own definition. Isn't the definition whatever you want it to be according to what you want to do?
I guess it all depends on what you call adjacent for feature vectors. Does it even mean anything to spay that two features vectors are adjacent? For that you need to define a total ordering on your feature vector set? Is an ordering meaningful for feature vectors?
Traditionally, you would calculate the euclidean distance between all pairs of the set, something you can do with pdist, or yourself easily:
dist = sqrt(sum(permute(A, [1, 3, 2]).^2 - permute(A, [3, 1, 2]) .^ 2, 3)
dist(i, j) is the euclidean distance between vector A(i) and A(j).

1 Comment

@Guillaume very sorry for the confusion, could you refer to my reply to @Image Analyst above?

Sign in to comment.

Asked:

on 16 Aug 2019

Edited:

on 17 Aug 2019

Community Treasure Hunt

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

Start Hunting!