How to measure the euclidean distance of points continuously from point A to B (and point B to C)?

12 views (last 30 days)
Hi,
Currently, I have the following code, which I use to measure the euclidean distances from a 3-dimensional point to a reference point (here it is (:,10)) for each cell of doubles in cell_of_double_baskets like so:
H = @(x) sqrt(((x(:,1)-x(:,10)).^2) ... % euclidian distance formula
+ ((x(:,2)-x(:,10)).^2) ...
+ ((x(:,3)-x(:,10)).^2));
baskets_xyz_h_ref = cellfun( H, cell_of_double_baskets, 'uni',false ); % compute the distances
Now, I am trying to measure the distance from each point (x,y and z) to its previous point for every data point in the list in every cell of doubles.
Basically,
H = @(x) sqrt((x2-x1).^2) ...
+ (y2-y1).^2) ...
+ (z2-z1).^2));
but for the entire length of each cell in cell_of_double_baskets. In this case I would disregard column 10 of course.
How would that look? Do I need a for loop?
Thank you!

Accepted Answer

Voss
Voss on 19 Mar 2022
Edited: Voss on 19 Mar 2022
load('cell_of_double_baskets.mat')
cell_of_double_baskets{1}
ans = 1617×10
0.6930 1.6917 -2.0863 0.5340 0.7811 -2.1244 0.8558 0.8125 -1.9208 0 0.6930 1.6917 -2.0863 0.5343 0.7810 -2.1240 0.8552 0.8125 -1.9210 0 0.6929 1.6918 -2.0864 0.5344 0.7815 -2.1238 0.8553 0.8126 -1.9209 0 0.6927 1.6918 -2.0865 0.5349 0.7820 -2.1232 0.8546 0.8125 -1.9211 0 0.6926 1.6918 -2.0866 0.5354 0.7826 -2.1225 0.8540 0.8126 -1.9212 0 0.6925 1.6917 -2.0867 0.5359 0.7827 -2.1216 0.8537 0.8127 -1.9212 0 0.6923 1.6917 -2.0870 0.5367 0.7827 -2.1200 0.8529 0.8130 -1.9214 0 0.6923 1.6917 -2.0870 0.5371 0.7827 -2.1193 0.8525 0.8130 -1.9215 0 0.6921 1.6916 -2.0872 0.5371 0.7827 -2.1192 0.8526 0.8130 -1.9214 0 0.6921 1.6916 -2.0874 0.5375 0.7826 -2.1185 0.8523 0.8129 -1.9214 0
n = numel(cell_of_double_baskets);
d = cell(1,n);
for kk = 1:n
% take the difference of adjacent rows of cell_of_double_baskets{kk},
% first 3 columns only, square the differences, sum them along 2nd
% dimension (x,y,z), take the square root, store the result as d{kk}
d{kk} = sqrt(sum(diff(cell_of_double_baskets{kk}(:,1:3),1,1).^2,2));
end
d
d = 1×19 cell array
{1616×1 double} {845×1 double} {3811×1 double} {529×1 double} {5462×1 double} {600×1 double} {997×1 double} {1124×1 double} {644×1 double} {954×1 double} {1746×1 double} {964×1 double} {545×1 double} {1210×1 double} {3022×1 double} {940×1 double} {914×1 double} {2941×1 double} {978×1 double}
d{1}
ans = 1616×1
1.0e+00 * 0 0.0001 0.0002 0.0002 0.0002 0.0004 0 0.0002 0.0002 0.0002
  2 Comments

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 19 Mar 2022
I think you could use a loop
curveDistance = 0;
dx = diff(x);
dy = diff(y);
dz = diff(z);
for k = 1 : length(dx)
curveDistance = curveDistance + sqrt(dx(k)^2 + dy(k)^2 + dz(k)^2);
end
  8 Comments
Image Analyst
Image Analyst on 19 Mar 2022
What about using a 2-D array
% Make 2d array with enough columns to hold any array, like the max of
% length(dx) you ever expect to encounter.
curveDistance = zeros(numel(cell_of_double_baskets), 100000)
lastColumn = 100000;
for k2 = 1 : numel(cell_of_double_baskets) % For every cell in the cell array
thisCellContents = cell_of_double_baskets{k2}; % Get contents of cell
% For example if thisCellContents is an N-by-3 array of x,y,z columns
x = thisCellContents(:, 1);
y = thisCellContents(:, 2);
z = thisCellContents(:, 3);
curveDistance(k2) = 0;
dx = diff(x);
dy = diff(y);
dz = diff(z);
curveDistance(k2, 1) = sqrt(dx(1)^2 + dy(1)^2 + dz(1)^2);
for k = 2 : length(dx)
curveDistance(k2, k) = curveDistance(k2, k-1) + sqrt(dx(k)^2 + dy(k)^2 + dz(k)^2);
end
lastColumn = max([length(dx), lastColumn]); % Keep track of the most number of columns we're going to use.
end
% Crop off unused columns.
curveDistance = curveDistance(:, 1:lastColumn);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by