How to measure the euclidean distance of points continuously from point A to B (and point B to C)?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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!
0 commentaires
Réponse acceptée
Voss
le 19 Mar 2022
Modifié(e) : Voss
le 19 Mar 2022
load('cell_of_double_baskets.mat')
cell_of_double_baskets{1}
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}
2 commentaires
Voss
le 19 Mar 2022
I edited my answer after seeing your comment on the other answer. Please check my answer again.
Plus de réponses (1)
Image Analyst
le 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 commentaires
Image Analyst
le 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);
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!