I hope my problem has a very simple solution. I just can’t find it:
Assume you have two vectors (one a column vector, one a row vector) A, B:
A = [1,2,3]
B = [4;5;6]
if we multiply them as follows, we get a matrix:
>> B*A
ans =
4 8 12
5 10 15
6 12 18
Now my problem is: I have two 3D matrices of sizes m × n × p and m × n × q
Imagine along dimensions m and n we have pixels and for each pixel we have a vector (length p or q).
Now what I want, is to multiply for every corresponding pixel the vectors of the two images, such that for every pixel I get a matrix and thus in total a 4D Matrix in the end.
How do I do this efficiently?
Loops in Matlab are no longer a thing to be feared, or avoided per se.
Granted, great care should be taken when using them, but nevertheless, the JIT can take care of many kinds of loops, improving performance even beyond builtin functions.
Consider the following test cases:
Results:
Regardless of the performance, I also find the last case a lot more intuitive and readable than the non-loop case.