How to vectorize a function that accepts a matrix and returns a vector in Matlab/octave/python

79 Views Asked by At

Given a function, that accepts a matrix and returns a vector: $g(X):\mathbb{R}^{nxm}\rightarrow \mathbb{R}^{m}$, s.t. $g^{(i)} = t\cdot X^{(i)}$; where $X$ is an $n\times m$ matrix, $t$ is a row vector in $\mathbb{R}^{n}$, $X^{(i)}$ is the $i$-th column vector of $X$, also $n$-dimensional. $g^{(i)}$ is the $i$-th component of the $m$-dimensional vector $g$, hence $i$ runs from 1 to $m$.

If we are given numerically the entries of $X$ and $t$, is it possible to compute the entries of $g$ in a vectorized, non-loop way?

In, e.g., Matlab/Octave using a loop one can do:

for i=1:m 
   g(i) = dot(t,X(:,i));
end

Is it possible to do this in a vectorized way without the loop in Matlab or Python?

1

There are 1 best solutions below

3
On BEST ANSWER

You can use the library numpy.dot() in python.