File Exchange

image thumbnail

Vector algebra for arrays of any size, with array expansion enabled

version (88.7 KB) by Paolo de Leva
Multiple dot, cross, and outer products, cross divisions, norms, normalizations, projections, etc.


Updated 26 Feb 2009

View Version History

View License

This toolbox was written to complete the incomplete set of vectorial operations provided with MATLAB, and to enhance the features of two of them (DOT and CROSS), by enabling virtual array expansion (AX). AX is enabled in all the binary operations included in this toolbox, and allows you, for instance, to multiply a single vector with an array of vectors, by virtually replicating the vector to match the size of the array.

All the functions in this toolbox can automatically operate on block arrays (arrays of any size containing vectors along one of their dimensions).

MATLAB includes four functions which can apply binary operations on the vectors contained in two block arrays:

+ (generic operator usable to perform vector additions)
- (generic operator usable to perform vector subtractions)
DOT (specific function performing dot products)
CROSS (specific function performing cross products)

They all perform multiple operations, but they cannot perform automatic AX. MATLAB also includes function SUM, which can be used to add together two or more vectors contained in a single matrix or N-D array. For instance, if A is a MN matrix, regarded as a concatenation of N column vectors, these vectors can be added together by using SUM(A, 2), which yields an M1 matrix.

DOT2 and CROSS2, included in this toolbox, enable AX for dot and cross products.

Outer products, orthogonal cross divisions, and generalized cross divisions with AX enabled, can be performed by calling functions OUTER, OCD, and CROSSDIV, included in this toolbox.

As for cross divisions (CDs), they were not invented until now (see Weisstein, 2008), but I needed them, so I defined and implemented them (de Leva, 2008). They determine one of the infinitely many anticrossproducts of the cross product a  b (see screenshot). They were not indispensable, but markedly simplified my equations and my code, and those who appreciate short symbolic equations are likely to love them. Without CDs, in some cases you are forced to write scalar equations, containing negative terms and operations involving various combinations of scalar components. In other cases, you need to write longer vectorial equations. Those equations are not complex, but they are certainly less simple than a CD. For instance, a typo is more likely to occur when you write scalar equations than when you write a CD. See CROSSDIV help for more detailed explanations.

A list of simple vectorial operations is given below. Only the first five are implemented in intrinsic MATLAB functions. The others are implemented in this toolbox. All of them can operate on N-D arrays of vectors. See the respective help texts for further details.

Operation: MATLAB implementation
Repeated addition: SUM
Binary addition: +
Subtraction: -
Dot product: DOT
Cross product: CROSS

Operation: ARRAYLAB implementation
Euclidean norm: MAGN
Normalization: UNIT
Dot product (AX enabled): DOT2
Cross product (AX enabled): CROSS2
Outer product (AX enabled): OUTER
Orthogonal cross division (AX enabled): OCD
Generalized cross division (AX enabled): CROSSDIV
Projection (AX enabled): PROJECTION
Rejection (AX enabled): REJECTION

A detailed definition of AX is given in the manual of the MULTIPROD toolbox, published on MATLAB Central (file #8773).

The ARRAYLAB toolbox

The functions included in this toolbox comply with the ARRAYLAB philosophy, and are a part of the "ARRAYLAB toolbox" (see the manual of the MULTIPROD toolbox, MATLAB Central, file #8773).

Some of them call MULTIPROD. This function is a powerful generalization for N-D arrays of the MATLAB function MTIMES and the matrix multiplication operator (*).

MULTIPROD has a broad field of potential applications. For instance, it can use large arrays of 33 or 44 transformation matrices to perform, in a single step and with no loops, multiple geometrical transformations (rotations, roto-translations) on arrays of vectors. Thus, it deserves a separate introduction and I published it in a separate package on MATLAB Central (file #8773).

Some other functions of this toolbox call BAXFUN, another function which belongs to the ARRAYLAB toolbox and which is published separately on MATLAB Central (file #23084).

Output testing

The functions testDOT2, testCROSS2, testOUTER, testPROJECTION, testREJECTION, testUNIT, testXDIV, testOCD, stored in a separate folder "Testing", contain the code I used to test the output of the main functions. Since UNIT calls MAGN, testUNIT tests both UNIT and MAGN.


de Leva P., 2008. Anticrossproducts and cross divisions. Journal of Biomechanics, 8, 1790-1800 (

Weisstein E. W. Vector Division. From MathWorld--A Wolfram Web Resource. (Retreived on 2008 Dec 4)

Cite As

Paolo de Leva (2021). Vector algebra for arrays of any size, with array expansion enabled (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (12)

Josh Philipson

Thank you for sharing this work.

Denis Anikiev

Paolo de Leva

Dear Xiaodong, thank you for your offer, but I cannot include your function DOT3 in my package. The dot product requires the complex conjugate of its first operand to deal with complex vectors. Even the MATLAB function DOT applies it:


Notice that, for real arrays,


hence, your function DOT3 will give correct results only when operating on real vectors, while DOT2, included in my package, is supposed to give correct results for both real and complex vectors.

Moreover, your function does nothing else than calling MULTIPROD. It would be more convenient to call it directly, using syntax 4b:


For instance, if vectors are contained along dimension 2 of A and B, and you do not want to compute the conj(A), you can use

C = MULTIPROD(A, B, 2, 2)

or more simply


Paolo de Leva

There's a typo at the end of my previous comment. I should have written:

...if vectors are contained along dimension 2 of A and B, you can use

C = MULTIPROD(A, B, 2, 2)

or more simply



Paolo and users of this package: I made a code to do the dot product similar to dot2 but without applying complex conjugate of the first input array:

In this updated function, C=dot3(A,B)=transpose(A) dot product with B. In Paolo's function library, dot2(A,B)=transpose(conj(A)) dot product with B.

Download as needed. Paolo, welcome to include this function in your package :)

Mark Flynn

Paolo's Vector Algebra lab was the solution I have been looking for. This should be a part of the standard Matlab distribution because it realizes the full potential of what Matlab was meant to do.

Paolo de Leva

Mark Flynn posted this question elsewhere:

"...I have 2 sets of vectors where I want to get the inner product of each vector in the first set with each vector in the second. E.g. 24 10x1 vectors with 64 10x1 vectors... could you give me some suggestions for how I could do it?"

The Array expansion (AX) engine used by all the functions in this toolbox can solve this kind of problems quickly and efficiently.

DOT2 can perform the operation described by Mark Flynn with a single command, provided that you define the two block arrays as a 24x1x10 array A and a 1x64x10 array B. Both arrays contain 10-element vectors along dimension 3.

For instance:
A = rand(24,1,10)
B = rand(1,64,10)
PRODUCTS will be a 24 x 64 matrix.

See MULTIPROD manual for details about the Array expansion (AX) engine.
Enjoy my ARRAYLAB toolboxes.


Much much faster than the repmat --> vectorized dot product combination I was using with a large data set. It got called a lot. Thank you for the hours this code saves me!

Thomas Clark

Hi Pablo, Would you mind re-submitting the MULTIPROD function please? I think it has become lost in the re-jig of The Mathworks' site.


Tom Clark

rodolfo molina

Giuseppe Vannozzi

Excellent work, Paolo!

Pietro Picerno

something new to write in Maths' books: the cross division. Excellent!

MATLAB Release Compatibility
Created with R14
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!