Matlab

How to implement cross product in MATLAB?

Finding the product of two vectors is a widely used mathematical and physical operation to perform many mathematics and physics tasks. There are two methods to determine a product of two vectors. One is the scalar or dot product and the other is the cross or vector product. A scalar product is a physical quantity that returns a scalar value after multiplying two vectors. In comparison, the vector product is a physical quantity that returns a vector after multiplying two vectors.

Computing the product of large vectors is not an easy task. It may require large calculations and time while computing it manually. However, in today’s era of high computing tools, we are blessed with MATLAB which makes many calculations in the shortest amount of time using the built-in functions. One such function is the cross() which allows us to determine the cross product of two vectors.

This tutorial will discover:

What is the Cross-Product?

The cross-product of two vectors is a physical quantity that is calculated by multiplying two vectors. It returns a vector perpendicular to the given two vectors. If A and B are two vector quantities, their cross-product C is given as:

Where C is also a vector quantity and it is perpendicular to both A and B.

Why We Need to Determine the Cross product?

The cross-product performs many tasks in physics, mathematics, and engineering. Some of them are given below.

The cross-product is used to find:

  • The area of a triangle.
  • The angle between two vectors.
  • A unit vector perpendicular to two vectors.
  • The area of a parallelogram.
  • Collinearity between two vectors.

How to Implement the cross product of Two Vectors in MATLAB?

MATLAB facilitates us with a built-in cross() function to find the cross product of two vectors. This function accepts two vectors as mandatory inputs and provides their cross-product in terms of vector quantity.

Syntax

The cross() function can be implemented in MATLAB through the given ways:

C = cross(A,B)

C = cross(A,B,dim)

Here,

The function C = cross(A,B) is responsible for calculating the cross product C of the given vectors A and B.

  • If A and B represent vectors, they must have a size equal to 3.
  • If A and B represent two matrices or multidirectional arrays, they must have the same size. In this situation, the cross() function considers A and B as a collection of vectors having three elements and calculates their cross product along the first dimension having a size equal to 3.

The function C = cross(A,B,dim) is responsible for calculating the cross product C of the given two arrays A and B along the dimension dim. Keep in mind that A and B must be two arrays having the same size and size(A,dim), and size(B,dim) must be equal to 3. Here, dim is a variable containing a positive scalar quantity.

Examples

Consider some examples to understand the practical implementation of the cross() function in MATLAB.

Example 1: How to Determine Cross Product of Two Vectors?

In this example, we calculate the cross-product C of the given vectors and using the cross() function.

A = [-7 9 2.78];

B = [1 0 -7];

C = cross(A,B)

Now we can verify our result C by taking its dot product with the vectors A and B. If C is perpendicular to both vectors A and B it implies C is a cross product of A and B. We can check the perpendicularity of C with A and B by taking its dot product with A and B. If the dot product of C with A and B equals 0, it implies C is perpendicular to A and B.

dot(C,A)==0 && dot(C, B)==0

After performing the above perpendicularity test, we obtained a logical value of 1 that implies the above operation is true. Hence, we conclude that the resultant vector C represents the cross-product of the given vectors A and B.

Example 2: How to Determine the Cross Product of Two Matrices?

The given example calculates the cross-product C of the given matrices A, created using the magic() function, and B, a matrix of random numbers, using the cross() function. Both matrices A and B are equal in size.

A = magic(3);

B = rand(3,3);

C = cross(A,B)

As a result, we obtain a 3-by-3 matrix C that is the cross-product of A and B. Each column of C represents the cross product of the respective columns of A and B. For example, C(:,1) is the cross product of A(:,1) and B(:,1).

Example 3: How to Find Cross Product of Two Multidirectional Arrays?

The given MATLAB code determines the cross-product C of the given multidirectional arrays A, an array of random integers, and B, an array of random numbers, using the cross() function. Both arrays A and B are equal in size.

A = randi(100,3,4,2);

B = randn(3,4,2);

C = cross(A,B)

As a result, we obtain a 3-by-4-by-2 array C that is the cross-product of A and B. Each column of C represents the cross product of the respective columns of A and B. For example, C(:,1,1) is the cross product of A(:,1,1) and B(:,1,1).

Example 4: How to Find the Cross Product of Two Multidirectional Arrays Along the Given Dimension?

Consider arrays A and B from Example 3 having size 3-by-3-by-3 and use the cross() function to find their cross product along dimension dim=2.

A = randi(100,3,3,3);

B = randn(3,3,3);

C = cross(A,B,2)

As a result, we obtain a 3-by-3-by-3 array C that is the cross-product of A and B. Each row of C represents the cross product of the respective rows of A and B. For example, C(1,,1) is the cross product of A(1,:,1) and B(1,:,1).

Conclusion

Finding the cross product of two vectors is a common operation widely used in mathematical and engineering tasks. This operation can be performed in MATLAB using the built-in cross() function. This guide has explained the different ways to implement the cross-product in MATLAB using multiple examples.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.