MATLAB Answers

0

How to calculate the distance

Asked by Asad Abbas on 13 Mar 2019
Latest activity Commented on by Asad Abbas on 13 Mar 2019
I have table B as given bellow. I have another point for example A=[100 111 80 120]. I want to calculate the euclidean distance between A and the columns (fourth, fifth, sixth and seventh) of B for each row.
B=
0 1 0 153 119 97 148
0 1 0 148 122 98 149
0 1 0 163 126 95 150
0 1 0 188 150 118 178

  2 Comments

... for each row ? What do you mean ?
matric B has four rows. I want to claculate the deistance between A and the last four columns of B
such as distance between
153 119 97 148 and A=[100 111 80 120]
148 122 98 149 and A=[100 111 80 120]
163 126 95 150 and A=[100 111 80 120]
188 150 118 178 and A=[100 111 80 120]

Sign in to comment.

2 Answers

Answer by Torsten
on 13 Mar 2019
 Accepted Answer

distmat = B(:,4:7)-repmat(A,4,1);
dist = vecnorm(distmat.')

  4 Comments

Show 1 older comment
This code should work. Another option is:
distmat = B(:,4:7)-A;
dist = vecnorm(distmat.')
Works for me:
A = [100 111 80 120];
B = [0,1, 0, 153, 119, 97, 148;...
0, 1, 0, 148, 122, 98, 149;...
0, 1, 0, 163, 126, 95, 150;...
0, 1, 0, 188, 150, 118, 178];
distmat = B(:,4:7)-repmat(A,4,1);
dist = vecnorm(distmat.')
Now its working.
Thank you so much.

Sign in to comment.


Answer by Alex Mcaulley on 13 Mar 2019

What do you mean by distance? euclidean, just the difference...
If you are looking for the difference:
B = [0 1 0 153 119 97 148;
0 1 0 148 122 98 149;
0 1 0 163 126 95 150;
0 1 0 188 150 118 178];
A = [100 111 80 120];
res = B(:,4:7)-A;

  1 Comment

I mean euclidean distance.

Sign in to comment.