error using cross (A and B must have at least one dimension of length 3.)

47 views (last 30 days)
Serim Park on 23 Sep 2020 at 8:42
Commented: Serim Park on 23 Sep 2020 at 10:43
I want to do cross product with 2 matrices (size : 25000X1) but it didn't work.
(A and B must have at least one dimension of length 3.)
Is there any way to find the cross product of those two matrices?

1 Comment

Ameer Hamza on 23 Sep 2020 at 8:59
How do you define the cross product in 25000-dimensional space?

John D'Errico on 23 Sep 2020 at 9:47
Edited: John D'Errico on 23 Sep 2020 at 10:14
I'm not sure what you want in terms of a 25000 dimensional cross produt between two vectors. What do you think it means? After all, there are multiple uses for a cross product.
A cross product between two vectors produces a result that has TWO important aspects that I can think of. Which aspect is important to you?
First, and generally foremost, cross(A,B) produces a vector (in 3-d) where using right hand rule a third vector that points in a direction orthogonal to the two. This vector points in a direction that is usually described as the right hand rule. The problem is, in more than 3 dimensions, thus N dimensions, the vector orthogonal to TWO vectors is actually a set of N-2 vectors. You can use a tool like null, for example, to compute the array which will be of size 24998x25000, of orthogonal basis vectors that will span the null space of the two indicated vectors. Of course in 25000 dimensions, right hand rule is meaningless. And it is also true that those basis vectors will not be unique. ANY set of orthogonal basis vectos of the nullspace are equivalent. But if thos is what you want to do, then just use null.
Next, some people may want to use a vector cross product to determine if two vectors are orthogonal. This would seem a terribly bad idea in higher dimensions, in the sense that it uses a computation that is numerically poor (let me call it unstable) to determine if the vectors are orthogonal. So if all you want is to know if the vectors are orthognal, you are barking up the wrong tree here. Why is is a bad idea? The answer derives from the next use for a cross product I'll describe.
Thus some people want to use a cross product to compute an area (or volume in higher dimensions.) Again, lets twlk 3-d. Here the MAGNITUDE of the cross product between two vectors indicates the area of a 2-d parallelogram. (Divide that by two, and you get the area of a specific triangle.) That is, given three points A,B,C that live in a 3dimensinal space,
norm(cross(B-A,C-A))/2
gives the area of the triangle BAC. A nice trick that people use to compute a triangle area. But it does not make as much sense in 4-d or higher, unless you explicitly define what volume you are now trying to compute. And in higher dimensions cross products will become problemaic for jobs like this, because volumes get nasty in high dimensions. What do I mean? The volume of an n-dimensional object grows very rapidly, in fact exponentially. It also goes to zero exponentially fast. Either way, in 25000 dimensions, the volume of anything you will compute in 25000 dimensions is either zero or it is inf, when computed in double precision.
For example, what is the volume of a hyper-cube in 25000 dimenions? Consider a hyper-cube of side length 2 in each dimension? That 25000 dimensional volume would be 2^N, where N=25000. And that would overfow to inf, when you tried to represent it in double precision arithmetic. But now consider the relative volume of a simplex in N dimensions, with side lengths 2? Since we can dissect the N-hypercube into factorial(N) such rotationally equivalent simplexes, then the volume would seem to be 1/factorial(N) of the corresponding hypercube. Sorry, but double precision arithmetic just goes to hell in high dimensions, at least when you might try to use these magnitudes to compute anything.
We can see all of this happening when I do a simple computaiton, one that I often use to explain why a determinant is such a bad idea in higher dimensions. Thus, what is
>> det(eye(25000))
ans =
1
I hope you can know that without even needing MATLAB. It is just 1. It took my computer a few seconds to do, as it is a highly CPU intensive computation. But now try these two similar computations:
>> det(2*eye(25000))
ans =
inf
>> det(0.5*eye(25000))
ans =
0
In either case, we have managed to either overflow or underflow double precision arithmetic to perform a simple task.
You need to see that determinant essentially can be viewed as a volume, in a high number of dimensions.
My point is, IF you think you want to compute the cross product in high dimensions to compute a volume, you probably need to re-think what you are doing.
Really, you need to think about what it is that you think you want to compute. WHY are you wanting to compute the cross product? There are things you can do, but until you carefully define what aspect of the cross product you want to use and why, you really can't do much.

1 Comment

Serim Park on 23 Sep 2020 at 10:43
Thank you so much for your help.
Thanks to you, I just realized what I missed and how to change the code.

KSSV on 23 Sep 2020 at 9:02