I want to compute y = a⊗a⊗a, where a is a n-by-1 vector, and ⊗ is the outer product operator. In this case y should be an n-by-n-by-n tensor.
If y = a⊗a, it is easy. I simply do:
y = a * a'
But what to do in the first case? How do I compute this outer product efficiently in MATLAB if there are more than two vectors?
In a multi-dimensional (tensor) case of
y = u⊗v, I believe that you need to shift the dimensions of the second operand like so:and then multiply them with
bsxfun:The regular matrix multiplication is defined only for vector and 2-D matrices, so we couldn’t use it in the general case.
Also note that this computation still fails if the second operand is a 1-D vector, because
ndimsreturns 2 instead of 1 for vectors. For this purpose, lets define our own function that counts dimensions:To complete the answer, you can define a new function (e.g. an anonymous function), like so:
and then use it as many times as you want. For example,
y = a×a×awould be computed like so:Of course, you can write a better function that takes a variable number of arguments to save you some typing. Something along these lines:
I hope I got the math right!