MATLAB Answers

sarah nik

How to calculate distance from one point base to multipoints and save it as a row?

Asked by sarah nik
on 24 Jun 2013

Hello Everyone,

I need your help in order to overcome my problem, here is my code:

AP_dist = zeros(N);
       for i = 1:N
          for j=1:1
               p=line([MS_loc(i,1) AP_pos(1,1)], [MS_loc(i,2)     AP_pos(1,2)],'Marker','.','LineStyle','-.');
               X= [MS_loc(i,1),AP_pos(1,1);MS_loc(i,2),AP_pos(1,2)];
                AP_dist= pdist(X,'euclidean')


Matt J
on 24 Jun 2013

And the problem is?

ihave N=5. the problem is. AP_dist just save the latest value. it should have 5 value as my N=5.


No products are associated with this question.

2 Answers

Answer by Andrei Bobrov
on 24 Jun 2013
Edited by Andrei Bobrov
on 24 Jun 2013
 Accepted answer

AP_dist= sqrt(sum(bsxfun(@minus,MS_loc,AP_pos).^2,2));


for jj = size(MS_loc,1):-1:1
    AP_dist(jj,1) = pdist([MS_loc(jj,:);AP_pos]);


AP_dist = arrayfun(@(ii)pdist([MS_loc(ii,:);AP_pos]),(1:size(MS_loc,1))');

  1 Comment

thank you very much!! ive tried all of them, and all works well

Answer by Walter Roberson
on 24 Jun 2013

AP_dist(i) = pdist(X,'euclidean');

  1 Comment

thank you for this. now i wonder whether i use the correct formula to calculate distance. because it gives different value. please help me AP_dist(i) = pdist(X,'euclidean'); or AP_dist(i) = sqrt((MS_loc(i,1) - AP_pos(j,1))^2 ... + (MS_loc(i,2) - AP_pos(j,2))^2);

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!