Angles from XY coordinates from a matrix

조회 수: 11 (최근 30일)
Michael
Michael 2012년 11월 13일
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.

채택된 답변

Matt Fig
Matt Fig 2012년 11월 13일
편집: Matt Fig 2012년 11월 13일
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
Michael
Michael 2012년 11월 14일
Thank you so much. This worked perfectly!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jürgen
Jürgen 2012년 11월 13일
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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by