Angles from XY coordinates from a matrix

6 visualizaciones (últimos 30 días)
Michael
Michael el 13 de Nov. de 2012
Hello,
I am generally new to matlab, and any help would be appreciated.
I am currently trying to collect data on joint angles over time. I have figured out how to get joint data from XY coordinates at a single point in time. I would like matlab to be able to calculate joint angle at each time interval.
Here is some example data...
[x1 y1 x2 y2 x3 x4
23 32 45 56 66 54
24 34 46 55 66 55
22 36 44 54 65 56
33 23 43 57 64 53]
Here is the matlab code I have working with...
x1=a(1,1);
y1=a(1,2);
x2=a(1,3);
y2=a(1,4);
x3=a(1,5);
y3=a(1,6);
x1=23;
y1=32;
x2=45;
y2=56;
x3=66;
y3=54;
% Create vectors describing sides of triangle
P1=[x1 y1];
P2=[x2 y2];
P3=[x3 y3];
P12=P2-P1;
P13=P3-P1;
% Calculate the angle between two of the sides
ang = acosd(dot(P12,P13)/sqrt(sum(P12.^2)*sum(P13.^2)))
Matlab will now pump out the angle for line 1 of the data above.
I would like to be able to have a code that will read the data from a matrix, and then pump out the angles from each line.
Example:
ang=
20.049
20.056
..........
..........
etc.
Thank you so much for your assistance.

Respuesta aceptada

Matt Fig
Matt Fig el 13 de Nov. de 2012
Editada: Matt Fig el 13 de Nov. de 2012
Seems like a simple FOR loop would do the trick.
for ii = size(a,1):-1:1
P12 = [a(ii,3)-a(ii,1), a(ii,4)-a(ii,2)];
P13 = [a(ii,5)-a(ii,1), a(ii,6)-a(ii,2)];
V(ii) = acosd(dot(P12,P13)/sqrt(sum(P12.^2)*sum(P13.^2)));
end
Or even a vectorized version:
P12 = [a(:,3)-a(:,1), a(:,4)-a(:,2)];
P13 = [a(:,5)-a(:,1), a(:,6)-a(:,2)];
V = acosd(dot(P12,P13,2)./sqrt(sum(P12.^2,2).*sum(P13.^2,2)))
  1 comentario
Michael
Michael el 14 de Nov. de 2012
Thank you so much. This worked perfectly!

Iniciar sesión para comentar.

Más respuestas (1)

Jürgen
Jürgen el 13 de Nov. de 2012
Hi,
two possibilities if you want to process a lot of data
: you can use loops or vectorize,
in this case, with few iterations I would use a for loop
so for Counter= 1: NumberOfRowsInMatrixa ... assign values ... do your calculations ... assign results to ResultsMatrix
end
regardsJ

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by