3*3 matrix in loop - calculation give NaN, why ?
1 view (last 30 days)
Show older comments
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))
0 Comments
Answers (1)
Mischa Kim
on 18 Feb 2014
Edited: Mischa Kim
on 18 Feb 2014
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 Comments
Mischa Kim
on 18 Feb 2014
What exactly goes on and why is hard to say from a distance. Can you attach your data and complete code?
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!