Computing six dot products involving one common input vector I have created a Matlab Simulink model with a series of dot products.



Embedded Coder generates a for loop for each dot product, as evidenced by this excerpt of the generated output:


/* DotProduct: '&lt;S3&gt;/Dot Product1' incorporates:
 *  Inport: '&lt;Root&gt;/LngPwr'
 */
EPAR_SPAF_dot_B.DotProduct1 = 0.0F;
for (temp_ = 0; temp_ &lt; 24; temp_++) {
  EPAR_SPAF_dot_B.DotProduct1 += temp_DataTypeConversion[temp_] *
  LngPwr[temp_];
}
/* End of DotProduct: '&lt;S3&gt;/Dot Product1' */

/* DotProduct: '&lt;S3&gt;/Dot Product2' incorporates:
 *  Inport: '&lt;Root&gt;/ShrtPwr'
 */
EPAR_SPAF_dot_B.DotProduct2 = 0.0F;
for (temp_ = 0; temp_ &lt; 24; temp_++) {
  EPAR_SPAF_dot_B.DotProduct2 += temp_DataTypeConversion[temp_] *
  ShrtPwr[temp_];
 }
/* End of DotProduct: '&lt;S3&gt;/Dot Product2' */



Can I improve the Simulink model such that it will compile to better code?

I want to avoid using embedded matlab or a S-function for the purposes of maintainability.
There is a common mathematical operation involving many dot products: it's called matrix multiplication.

$$ \left[\begin{matrix}v_1 &amp; v_2 &amp; v_3 &amp; v_4\end{matrix}\right]
\left[\begin{matrix}
a_1 &amp; b_1 &amp; c_1 \\
a_2 &amp; b_2 &amp; c_2 \\
a_3 &amp; b_3 &amp; c_3 \\
a_4 &amp; b_4 &amp; c_4
\end{matrix}\right] =
\left[\begin{matrix}(\vec{v}\cdot\vec{a}) &amp; (\vec{v}\cdot\vec{b}) &amp; (\vec{v}\cdot\vec{c})\end{matrix}\right]
$$

So, I recommend that you concatenate vectors \$\color{lightgreen}1\$ to \$\color{lightgreen}6\$ into a matrix and perform matrix multiplication.



You can also transpose and swap the matrices:

$$
\left[\begin{matrix}
a_1 &amp; a_2 &amp; a_3 &amp; a_4 \\
b_1 &amp; b_2 &amp; b_3 &amp; b_4 \\
c_1 &amp; c_2 &amp; c_3 &amp; c_4 \\
\end{matrix}\right]
\left[\begin{matrix}v_1 \\ v_2 \\ v_3 \\ v_4\end{matrix}\right]
 =
\left[\begin{matrix}\vec{a}\cdot\vec{v} \\ \vec{b}\cdot\vec{v} \\ \vec{c}\cdot\vec{v}\end{matrix}\right]
$$