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

# Thread Subject: Calculating distance to multiple points

 Subject: Calculating distance to multiple points From: Kate Date: 12 Jun, 2013 18:04:19 Message: 1 of 3 Hello! I am very new to programming. I have a few questions on how to set up some calculations I need to implement. Here is all I have. The integers below I am using are just as an example. My 3-D vetors are in the form of (x,y,z). However, I do not know if I set it up correctly to reflect that. There should be 5 anchors and 9 seats all in the form of a location (x,y,z). >> Anchors = [1 1 1; 2 2 2; 3 3 3; 4 4 4; 5 5 5] Anchors =      1 1 1      2 2 2      3 3 3      4 4 4      5 5 5 >> Seats = [1 2 3; 2 3 4; 3 4 5; 4 5 6; 5 6 7; 6 7 8; 7 8 9; 8 9 10; 9 10 11] Seats =      1 2 3      2 3 4      3 4 5      4 5 6      5 6 7      6 7 8      7 8 9      8 9 10      9 10 11 If that represents what I am trying to state, I have a few more questions. How do I write a code to calculate the distance between, lets say, Seat [1 2 3] to all 5 anchors? Then, I need to keep that distance and calculate the same thing will all of the Seats. Any help would be greatly appreciated!! -Kate
 Subject: Calculating distance to multiple points From: Josh Meyer Date: 12 Jun, 2013 18:47:50 Message: 2 of 3 >> How do I write a code to calculate the distance between, lets say, Seat >> [1 2 3] to all 5 anchors? The Euclidean distance between two points is equal to the 2-norm of the difference. So the distance from the point [1 2 3] to the point [1 1 1] is equal to sqrt(0^2 + 1^2 + 2^2), or sqrt(5). In MATLAB this is norm([1 2 3] - [1 1 1]). >> Then, I need to keep that distance and calculate the same thing will all >> of the Seats. There is probably a way to do this without so many loops, but one way to do it is: --------------------------------------------------- D = zeros(length(Seats),length(Anchors)); for i = 1:length(Seats)     for j = 1:length(Anchors)         D(i,j) = norm(Seats(i,:) - Anchors(j,:));     end end --------------------------------------------------- D =     2.2361 1.4142 2.2361 3.7417 5.3852     3.7417 2.2361 1.4142 2.2361 3.7417     5.3852 3.7417 2.2361 1.4142 2.2361     7.0711 5.3852 3.7417 2.2361 1.4142     8.7750 7.0711 5.3852 3.7417 2.2361    10.4881 8.7750 7.0711 5.3852 3.7417    12.2066 10.4881 8.7750 7.0711 5.3852    13.9284 12.2066 10.4881 8.7750 7.0711    15.6525 13.9284 12.2066 10.4881 8.7750 The first row of D is the distance from the first Seat, [1 2 3], to each Anchor, the second row is the distance from the second Seat, [2 3 4], to each Anchor, etc...
 Subject: Calculating distance to multiple points From: Gene Date: 12 Jun, 2013 19:19:09 Message: 3 of 3 Hi Josh: Let A = 3 \times na be an array with the coordinates of anchor 'j' in the jth column. and similarly for S = 3 \times ns D = repmat(S(:, i), 1, na) - A; % each column is a three-vector,                                 % giving the Euclidean representation                                 % of the vector from anchor 'j'                                 % to seat 'i' d = (sum(D.^2)).^(.5); % D.^2 is the component-wise square of D,                                 % sum gives the column sum(s)                                 % .^(.5) takes the square root of elements                                 % of d d is a row d(j) is the Euclidean distance from seat 'i' to anchor 'j'. Use a for loop on 'i' for the seats of interest. gene "Josh Meyer" wrote in message ... > >> How do I write a code to calculate the distance between, lets say, Seat > >> [1 2 3] to all 5 anchors? > > The Euclidean distance between two points is equal to the 2-norm of the > difference. So the distance from the point [1 2 3] to the point [1 1 1] is > equal to sqrt(0^2 + 1^2 + 2^2), or sqrt(5). In MATLAB this is norm([1 2 3] - > [1 1 1]). > > >> Then, I need to keep that distance and calculate the same thing will all > >> of the Seats. > > There is probably a way to do this without so many loops, but one way to do > it is: > --------------------------------------------------- > D = zeros(length(Seats),length(Anchors)); > for i = 1:length(Seats) > for j = 1:length(Anchors) > D(i,j) = norm(Seats(i,:) - Anchors(j,:)); > end > end > --------------------------------------------------- > D = > > 2.2361 1.4142 2.2361 3.7417 5.3852 > 3.7417 2.2361 1.4142 2.2361 3.7417 > 5.3852 3.7417 2.2361 1.4142 2.2361 > 7.0711 5.3852 3.7417 2.2361 1.4142 > 8.7750 7.0711 5.3852 3.7417 2.2361 > 10.4881 8.7750 7.0711 5.3852 3.7417 > 12.2066 10.4881 8.7750 7.0711 5.3852 > 13.9284 12.2066 10.4881 8.7750 7.0711 > 15.6525 13.9284 12.2066 10.4881 8.7750 > > The first row of D is the distance from the first Seat, [1 2 3], to each > Anchor, the second row is the distance from the second Seat, [2 3 4], to > each Anchor, etc...