Skip to content
Advertisement

Vectorizing Tensor Products from Python to Matlab

I am in the process of converting some code from Python into Matlab. I have code working that produces the same results, but I am wondering if there may be a way to vectorize some of my for loops in the Matlab code as it take a long time to run. X in an Nxd matrix, diff is an NxNxd tensor, kxy is an NxN matrix, gradK is an NxNx2 tensor, and sumkxy, dxkxy, and obj are all Nxd matrices.

Here is the original Python Code:

JavaScript

and here is my initial Matlab Code with all the for loops:

JavaScript

I know a way a faster way to calculate the kxy term is to use the following code:

JavaScript

But then I struggle on a way to then calculate gradK efficiently without diff. Any help or suggestions would be greatly appreciated!

Advertisement

Answer

If your goal is computation of obj you don’t need even to compute gradK and a:

JavaScript

gradK and dif can be computed this way:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement