Cross Product multi Dim data

17 views (last 30 days)
Bineet_Mehra
Bineet_Mehra on 29 Apr 2016
Commented: Bineet_Mehra on 30 Apr 2016
I have u1 v1 w1(velocity components) volumetric data. Size of u1 is NxNxN (N is 100) and same is true frol v1 and w1.
I have one more case of u2, v2, w2, each one of size NxNxN. both these data sets are obtained from TriScatteredInterp and meshgrid. Basically i plotted streamlines for these two cases and now i want to see how much deviation is there between these streamlines (from case 1 to 2.). I need to find the angle between them at the points given by meshgrid. But cross product wont work (A and B must have at least one dimension of length 3.).. any suggestion ?? Thanks a lot

Accepted Answer

Roger Stafford
Roger Stafford on 29 Apr 2016
Edited: Roger Stafford on 29 Apr 2016
Actually you can use matlab's 'cross' function.
A = cat(4,u1,v1,w1); % Combine the three components in the 4th dimension
B = cat(4,u2,v2,w2); % Ditto
C = cross(A,B,4); % Take the cross products there.
ang = atan2(sqrt(dot(C,C,4)),dot(A,B,4));
This last is taking atan2(norm(cross products),dot products), which is the accurate way of finding angles. The result should be a three dimensional NxNxN array of angles. The angles can range from 0 to pi radians.
  4 Comments
Roger Stafford
Roger Stafford on 29 Apr 2016
The array C, like A and B, is four dimensional with the fourth dimension of size 3. Each of those triplets along the fourth dimension are the components of the cross product of the corresponding pair of triplets in A and B. Now when we take dot(C,C,4), that is suddenly three dimensional with each element being the square of the length of the corresponding triplet in C. After we take the square root we have the lengths of each of the triplets in C. That accounts for the expression sqrt(dot(C,C,4)). The quantity dot(A,B,4) is also three dimensional and is the scalar (dot) product of each pair of corresponding triplets in A and B. As you undoubtedly are aware, the length of a cross product equals the product of the lengths of each separate vector times the sine of the angle between them. The scalar (dot) product is equal to the product of their lengths times the cosine of the angle between them. That is what is needed by the function 'atan2'. Its two arguments must be proportional to the sine and cosine, respectively, of the desired angle. Then atan2 will produce that angle. In your case it will produce a three dimensional array of NxNxN of such angles.
Bineet_Mehra
Bineet_Mehra on 30 Apr 2016
Hello Roger, Thanks for the brilliant explanation. Instead of taking N =100, i took N = 4 etc and did some hand calculations to understand and to hand calculate angles at few points. Everything is correct and understandable ie dot(C,C,4) having dim of NxNxN and cross(A,B,4) having dim of NxNxNx3 and how nicely you replaced norm(cross(A,B,4)) with sqrt(dot(C,C,4). Many thanks again . I think many people will benefit from your solution dealing with multi dimension matrices. cheers

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!