3*3 matrix in loop - calculation give NaN, why ?

조회 수: 1 (최근 30일)
N
N 2014년 2월 18일
댓글: Mischa Kim 2014년 2월 18일
I have a problem with calculating some data out a matrix that is in a loop. Below you can see the code. With this I can calculate a 3*3 matrix 202 times, which is relor. When I just let matlab show me this ( so no ; at the end), I can see that I get 202 times a 3*3 matrix. So this is good. However then I want to do some extra calculations on every matrix (KneeAngle), when I do this however I suddenly get NaN for every calculation of the KneeAngle. I cannot figure out why since I know that relor has data in it, matlab has shown me this already.
for i = 1:202;
origin2=(LE(:,i)+ME(:,i))/2;
Y2=(FH(:,i)-origin2)/norm(FH(:,i)-origin2);
ztemp2=(FH(:,i)-ME(:,i))/norm(FH(:,i)-ME(:,i));
x2=cross(Y2,ztemp2);
X2=x2/norm(x2);
z2=cross(X2,Y2);
Z2=z2/norm(z2);
R2=[X2 Y2 Z2];
midpoint=(LM(:,i)+MM(:,i))/2;
mid=(MLP(:,i)+MMP(:,i))/2;
Y3=(mid-midpoint)/norm(mid-midpoint);
ztemp3=(LM(:,i)-MM(:,i))/norm(LM(:,i)-MM(:,i));
x3=cross(Y3,ztemp3);
X3=x3/norm(x3);
z3=cross(X3,Y3);
Z3=z3/norm(z3);
R3=[X3 Y3 Z3];
relor(:,:,i)=R2\R3
KneeAngle(i:1) = asin(relor(3,2)); % Alpha
KneeAngle(i:2) = -atan((relor(3,1))/(relor(3,3))); % Beta
KneeAngle(i:3) = -atan((relor(1,2))/(relor(2,2))); % Gamma
plot(KneeAngle)
end
If I just give in the code below I do get a result ! but when I use i instead of a real number, its back to NaN. Can anybody help !?
r1=relor(:,:,15)
alpha=asin(r1(3,2))

답변 (1개)

Mischa Kim
Mischa Kim 2014년 2월 18일
편집: Mischa Kim 2014년 2월 18일
This can happen for a range of reasons. In your case I would suspect that some of the values in relor are zero. Dividing zero by zero will result in NaN.
One way to test if there are zeros in a matrix is
isequal(relor,find(relor))
which will result in a logic 0, if the matrix contains zeros.
  댓글 수: 2
N
N 2014년 2월 18일
편집: N 2014년 2월 18일
okay some are indeed zer0. But relor is done 202 times, some times its Nan other times it has data. So when I calculate the KneeAngle shouldn't I then also get a line of numbers that are sometimes NaN and other times give data ?
Mischa Kim
Mischa Kim 2014년 2월 18일
What exactly goes on and why is hard to say from a distance. Can you attach your data and complete code?

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by