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

2 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
lil brain
lil brain on 19 Mar 2022
@_ unfortunately you understood that slightly wrong. I am only interested in the first three columns of each cell [x y z]. Sorry if this wasnt made clear by me.
I am trying to get the distance/difference between the values in each consecutive row.
So basically
cell_of_double_baskets{1, 1} (2,x) - cell_of_double_baskets{1, 1} (1,x)
cell_of_double_baskets{1, 1} (3,x) - cell_of_double_baskets{1, 1} (2,x)
cell_of_double_baskets{1, 1} (4,x) - cell_of_double_baskets{1, 1} (3,x)
...
Does that make it clearer? (Also have a look at my last comment on the above answer @Image Analyst)

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
lil brain
lil brain on 19 Mar 2022
I see, you thought I wanted to add them up.
However, I need each distance individually.
I was trying to get a list of distances between each data point (x1-x2, x3-x2, x4-x3, etc.) for each cell.
Basically,
sqrt(dx.^2 + dy.^2 + dz.^2);
for each row for the entire length of each cell.
The result would be a list of distances (or differences) that looks like so:
list_of_distances_per_cell =
1×19 cell array
Columns 1 through 5
{1616×1 double} {845×1 double} {3811×1 double} {529×1 double} {5462×1 double} ... {nx1 double}
I hope this is more clear than before.
Ps: in cell_of_double_baskets only the first three columns for each cell are of importance. The others can be disregarded.
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.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!