title: Matlab: Looping a function on the elements of a 3-array vs Passing a 3-array to the same function
body: <p>In Matlab I have an array <em>v</em> of length <em>m</em>, a matrix of order <em>n</em> and a function <em>F</em> that takes as an input a <em>single</em> matrix and outputs a number. Starting from <em>v</em> I would like to apply the function to the whole array of matrices whose i-th element consists of a matrix M_i whose entries are obtained by multiplicating all the entries of <em>M</em> by <em>v_i</em>. The output would be itself an array of length n. </p>
  
  <p>As far as I can see there are two ways of achieving this:</p>
  
  <ol>
  <li><p>Looping on all i=1:n, computing <em>F</em> on all the <em>M_i</em>s and store all the corresponding values in an array</p></li>
  <li><p>Defining a 3-array structure that contains all the matrices M_i and correspondingly extending the function <em>F</em> as to act on 3-arrays instead of matrices. However this entails overloading some matrix operators and functions (transpose,  exponential, logarithm, square root, inverse etc...) as to formally handle a 3-array.</p></li>
  </ol>
  
  <p>I have done the simpler option 1. It takes a long time to execute. Number 2 promises to be faster- However, I am not sure if this is the case, and I am not familiar with overloading operators on Matlab. In particular: how to extend a matrix operator to a 3-array in such a way that it performs the related function on all of its entries. </p>