# Using @NLexpression in JuMP within a loop

**URL:** <https://discourse.julialang.org/t/using-nlexpression-in-jump-within-a-loop/9751>\
**Category:** Optimization (Mathematical)\
**Created:** [March 16, 2018, 4:25am UTC](https://discourse.julialang.org/t/using-nlexpression-in-jump-within-a-loop/9751 "2018-03-16T04:25:15Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![igorteo84](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/igorteo84/32/3727_2.png) [@igorteo84](https://discourse.julialang.org/u/igorteo84)\
**Post date:** [March 16, 2018, 4:25am UTC](https://discourse.julialang.org/t/using-nlexpression-in-jump-within-a-loop/9751/1 "2018-03-16T04:25:15Z")

</div>

Hello, I’ve a JuMP related question and how to use @NLexpression within a loop

mu is a JxN matrix that I’ve defined to be a variable in the MPEC model  
@variable(MPEC, mu[j=1:J, n=1:N], start=mu0[j,n]).

v2 is a 5Tx100 matrix, marketStarts and marketEnds are two Tx1 arrays, “x” is a Jx5 matrix and theta2 is 5x1. I’ve tried to use different sections of v2 to calculate different values for mu, but when I run the following code

for t=1:T  
v = v2[5\*(t-1)+1:5\*t,:];  
@NLexpression(  
MPEC,  
mu[j=marketStarts[t]:marketStarts[t],n=1:N],  
sum(theta2[k]\*x[j,k]\*v[k,n] for k=1:5)  
)  
end

the dimensions of mu change to 1xN, instead of JxN.

My question is how to do something similar to this inside @NLexpression:  
mu0 = Matrix{Float64}(J,N)  
for t=1:T  
v = v2[5\*(t-1)+1:5\*t,:];  
for n=1:N  
for j=marketStarts[t]:marketEnds[t]  
for k=1:K  
mu0[j,n] += theta2[k]\*x[j,k]\*v[k,n]  
end  
end  
end  
end

Thank you in advance and sorry in advance if I’m not being clear enough
