The colon operator in MATLAB doesn't do what you expect, as it serves another functionality. In fact, there is no built-in implementation for a double inner product in MATLAB. You need to implement it by yourself, for instance:
idx = max(0, ndims(A) - 1); %// Index of first common dimension
B_t = permute(B, circshift(1:ndims(A) + ndims(B), [0, idx - 1]));
double_dot_prod = squeeze(sum(squeeze(sum(bsxfun(@times, A, B_t), idx)), idx));
where A
and B
are your tensors (i.e multi-dimensional matrices). Vectorizing this was a hard nut to crack, so I hope I got the math right!
If you want, you can put this code in a function for convenience. For the sake of good practice, also verify that both tensors are of second rank or higher. Here's a friendly copy-paste version for you:
function C = double_dot(A, B)
assert(~isvector(A) && ~isvector(B))
idx = max(0, ndims(A) - 1);
B_t = permute(B, circshift(1:ndims(A) + ndims(B), [0, idx - 1]));
C = squeeze(sum(squeeze(sum(bsxfun(@times, A, B_t), idx)), idx));
A word of advice: I suggest that you read online tutorials to get yourself familiar with the basics of the MATLAB language.