How to create for loop for calculating angle between 3 3D points?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi there!
I want to create a for loop that calculates the angle between three 3D points. I have a double called 'data3D' that contains 1 row with several points in 3D space. col 1 = pt1_x, col 2 = pt1_y, col 3 = pt1_z, col 4 = pt2_x ..... For this calculation I only need the xyz coordinates for pt1.
data3D =
-.3 .2 -.3 -.5 .6 -.3
I have a double called 'Coordinates' that contains a varying number of rows, and constant number of columns. col 1 = x, col 2 = y, col 3 = z. The number of rows varies based on how many events occur in a single observation, but columns are consistent throughout.
Coordinates =
-.6 .5 -4.1
-.9 .4 -4.1
-1.1 .2 -4.2
I want my for loop to calculate the angle between my contact point (pt 1, data3D) and each subsequent point in Coordinates. i.e. I want to first calculate the angle between the vector (pt1 -> Coordinates(row1)) and the vector (pt1 -> Coordinates(row2)). The next calcuation should be the angle between vector (pt1 -> Coordinates(row2)) and the vector (pt1 -> Coordinates(row3)) and so forth.
For now, my for loop is structured like this, how would I structure the calculation formula?
nobservations1 = size(Coordinates,1);
icoordinates1 = nobservations1 - 1; %creates a variable that consists of number of observations -1
scanAngles = []; % create empty vector to store angles
for i_coord1 = 1:icoordinates1 %for each observation
curangle =
scanAngles = [scanAngles curangle];
end
Any help with this would be greatly appreciated, I'm a MATLAB novice and would love to learn!
0 Kommentare
Akzeptierte Antwort
Voss
am 11 Apr. 2024
p = data3D(1:3); % pt1
q = Coordinates;
v = q-p; % vectors from pt1 to each point in Coordinates
N = size(q,1);
angles = zeros(1,N-1);
for ii = 1:N-1
a = v(ii,:);
b = v(ii+1,:);
angles(ii) = acos(sum(a.*b)/(norm(a)*norm(b)));
end
Those angles are in radians; if you want angles in degrees, use acosd instead of acos.
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Geometric Transformation and Image Registration finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!