How to generate a 3D matrix from a 2D matrix through column-wise Hadamard product?

71 Views Asked by At

I have a matrix defined as, $ {\bf G}=[{\bf g}_1 \quad {\bf g}_2\quad...\quad{\bf g}_N], $ where ${\bf g}_i$ is a column vector of the length $N$. The tensor is defined as, ${\bf M}(i,j)={\bf g}_i \odot {\bf g}_j$, where $\odot$ is the Hadamard product. Is there any kind of matrix multiplication routine that can directly output this? Is there any way to efficiently calculate ${\bf M}$ in MATLAB?

1

There are 1 best solutions below

1
On BEST ANSWER

Note that $g_i$ is the $i^{th}$ column of $G,\:$ so the $k^{th}$ component of $g_i$ equals $G_{ki}$

The components of the desired third-order tensor are therefore $$M_{ijk} = G_{ki}\,G_{kj} \qquad \big({\rm no\;sum\;on\;} k\big)$$

I haven't used Matlab in years, but the above formula can be expressed in one line of Julia code using array comprehensions

M = [ G[k,i]*G[k,j]  for i=1:n, j=1:n, k=1:n ]

In Matlab you could use nested for-loops with the assignment M[i,j,k] = G[k,i]*G[k,j]

In Julia there's no penalty for using explicit loops, but I recall that Matlab users were always looking for ways to vectorize expressions to speed things up. Is that still the case?

Python also suffers from slow for-loops, but there are packages such as $\sf Einsum$ to handle tensor calculations. It's probably overkill for such a simple problem, but if those packages are available in Matlab they're likely faster than user-coded for-loops.