MATLAB Answers

When taking cross product I end up with NaN. how can i fix this?

23 views (last 30 days)
I wrote a bit of code that grabs 3 points on a randomly oriented platelet to create two lines so that i can calculate the normal vector to the platelet using cross product. the normal vectors I am getting do not seem right and some of them turn out to be NaN i was wondering if someone can help me fix this. thank you!
matrix = [(1:natom)',mol_tag,x,y,z];
mol_tag1 = matrix(:,2);
unique_mol_tags = unique(mol_tag1);
num_tags = length( unique_mol_tags );
mol_normals = zeros(num_tags,3);
for k = 1 : num_tags
three_idx = find(mol_tag1 == unique_mol_tags(k),3,'first');
coords = matrix(three_idx,3:5); % gets the first 3 coordinates from each mol_tag grouping
p1 = coords(1,:);
p2 = coords(2,:);
p3 = coords(3,:);
lin1 = [p2-p1]; %Creates 2 lines to form a plane
lin2 = [p3-p1];
u1 = cross(lin1,lin2); % calculate cross product to find normal vector of the plane
u = u1/norm(u1)
% u(isnan(u))=0; % setting the NaN two zero doesn't seem to work either
mol_normals(k , :) = u;
bqij = 0;
for i = 1:3
for j = 1:3
singleij(i,j) = u(i)*u(j);
bqij = bqij + singleij;
qij = (bqij-(1/3*(eye(3))))/(num_tags);

Accepted Answer

John D'Errico
John D'Errico on 3 Aug 2017
Edited: John D'Errico on 3 Aug 2017
No. You cannot "fix" it, in the sense that you will always get something with a NaN, using the code you have on the data you have. YOU might be able to fix the problem, in the sense that you can change your code to avoid the problem. For example, are you randomly sometimes selecting the same point twice in your selection?
NaNs result from several operations. For example, what is the result of inf-inf? Is it zero? Is it inf?
Likewise, what is 0/0? Again, it can arguably be ANY number, anything from 0 to 1 to 42 to inf, depending on how you take a limit.
How about inf/inf? Is it 1? 0? inf?
inf - inf
ans =
ans =
ans =
So your data has something in it that probably creates some infs (and therefore) some NaNs in it. Or it has a 0/0 operation in it. I cannot possibly know what did it, because I don't have your data.
Learn to use the debugger. It can help to detect where the problem arises.
help dbstop
dbstop if naninf
John D'Errico
John D'Errico on 3 Aug 2017
Easy enough to have this happen in your code.
lin1 = [1 2 3];
lin2 = [0 0 0];
u1 = cross(lin1,lin2); % calculate cross product to find normal vector of the plane
u = u1/norm(u1)
u =
If the vector u turns out to be zero, then you divide it by the norm of a vector of zeros. NaNs appear, as if by magic.
So easy enough. All you need to have happen is for ANY pair of points p1,p2,p3 to be identical. Then the resulting cross product will be a vector of zeros.
If p1==p2, then lin1 will be all zeros. If p1==p3, then lin2 will be all zeros. Either will create a vector of zeros as a cross product. Finally, the cross product of a vector with itself will be all zeros. That will happen when p2==p3.
Again, I don't have your data, so I cannot show you exactly how the problem arises. If you really want to know, then you need to use the debugger as I said. Set it to stop on the creation of a NaN or an inf. Then look carefully at what created the NaNs.

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!

Translated by