# How to improve performance in a function that repeatedly defines and multiplies matrices

**URL:** <https://discourse.julialang.org/t/how-to-improve-performance-in-a-function-that-repeatedly-defines-and-multiplies-matrices/108153>\
**Category:** Julia at Scale\
**Tags:** performance, parallel, linearalgebra, complex-numbers, matrix\
**Created:** [December 29, 2023, 9:47am UTC](https://discourse.julialang.org/t/how-to-improve-performance-in-a-function-that-repeatedly-defines-and-multiplies-matrices/108153 "2023-12-29T09:47:43Z")\
**Posts on this page:** 1\
**Showing post:** 59

<div class="post-metadata">

**Author:** ![Uranium238](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uranium238/32/209706_2.png) [@Uranium238](https://discourse.julialang.org/u/Uranium238)\
**Post date:** [January 5, 2024, 3:29pm UTC](https://discourse.julialang.org/t/how-to-improve-performance-in-a-function-that-repeatedly-defines-and-multiplies-matrices/108153/59 "2024-01-05T15:29:56Z")

</div>

> [@nilshg](#):
>
> where you repeatedly calculate things (and in the second example also seemingly throw away most of what you compute)

The outcome of the line `g = im*tr(W\X) + (transpose(W\V)*X*(W\V))[1] - (transpose(Y)*(W\V))[1]` is a `1x1` matrix. So I assumed writing `g[1]` would not be a problem. As it seemed like I am essentially converting a `Matrix` of dimensions `(1,1)` to a number.

---

_[View the full topic](https://discourse.julialang.org/t/how-to-improve-performance-in-a-function-that-repeatedly-defines-and-multiplies-matrices/108153)._
