I have an optimization function that receives a large vector `x` (let's say size 50,000) of parameters. My optimization strategy involves then coping the vector `x` into the diagonal of `M`, and then do some operation with `M` which is really quick.

This is the call:

```
[T, residual]   = fmincon(@fun, init_x_params, [], [], [], [], [], [], @confuneq, options2);

```

I realize that the bottleneck of my optimization function `fun` is actually the copy of the vector `x` into the diagonal of `M`, NOT the actual operation I do afterwards with `M`. Note that I update the diagonal in a smart way by using logical indexing using the vector of indices `indices` to be updated, it cannot be done faster I believe.

This is the optimization function, which is in the scope of the main function so has access to `M`.

```
function final_error = fun(x)
    % Updates M, this is the bottleneck!!
    M(indices) = x(:);

    % DO STUFF WITH M, it's fast!
end

```

I try to solve the bottleneck by passing the entire `M` as the vector to be optimize, but of course the problem is that I cannot pass the whole matrix `M`, because it's too big (and doesn't make sense to pass ALL the matrix, as I only update the diagonal). 

Is there any way to pass the diagonal of M directly to the optimization function? Or somehow pass the entire `M` but specify somehow that I only want to opimize its diagonal? Ideally I would want to use something like a 'reference' to the diagonal of `M` as a vector to optimize, but this is not possible in MATLAB I believe.