How to measure the euclidean distance of points continuously from point A to B (and point B to C)?
Ältere Kommentare anzeigen
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!
Akzeptierte Antwort
Weitere Antworten (1)
Image Analyst
am 19 Mär. 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 Kommentare
lil brain
am 19 Mär. 2022
Why? It's just the difference between adjacent elements so you would have one less. Look at this example for 4 elements
x = sort(randi(50, 1, 4))
dx = diff(x)
Image Analyst
am 19 Mär. 2022
I think that's simplest - easier to understand than cellfun(). So something like
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);
for k = 1 : length(dx)
curveDistance(k2) = curveDistance(k2) + sqrt(dx(k)^2 + dy(k)^2 + dz(k)^2);
end
end
lil brain
am 19 Mär. 2022
Image Analyst
am 19 Mär. 2022
Maybe a diagram would help. I'm assuming each cell has a list of x,y,z coordinates and you want to add up the length of all the line segments as you daisy-chain your way from one point to the next. So each cell would have one curve length. Apparently that is not what you have so can you make up a line diagram showing what distances are to be measured?
lil brain
am 19 Mär. 2022
Image Analyst
am 19 Mär. 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);
Kategorien
Mehr zu Common Operations finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!