# Converting a code with for a loop from MATLAB

In MATLAB I have the following for loop:

t=10;p=20;
n=-5:5;
[na,nb]=meshgrid(n,n);
dI=eye(length(n));
jj=(pi*2)*(na-nb);
F=sinc((na-nb)*t).exp(pi*t*1j*(na-nb));
cr=t*dI+p*(1-dI).*F;
i=1;
for kc=-pi:pi/20:pi
T1=(kc+2*pi*na).*(kc+2*pi*nb);
M=T1*inv(cr);
N=cr;
w=eig(M,N);
vy(:,i)=sqrt(real(w(1:t))).*(t/(2*pi));
i=i+1;
end


here is a my version in mathematica, but don't work :'(

na=[-5 -4 -3 -2 -1 0 1 2 3 4 5

-5 -4 -3 -2 -1 0 1 2 3 4 5

-5 -4 -3 -2 -1 0 1 2 3 4 5

-5 -4 -3 -2 -1 0 1 2 3 4 5

-5 -4 -3 -2 -1 0 1 2 3 4 5

-5 -4 -3 -2 -1 0 1 2 3 4 5

-5 -4 -3 -2 -1 0 1 2 3 4 5

-5 -4 -3 -2 -1 0 1 2 3 4 5

-5 -4 -3 -2 -1 0 1 2 3 4 5

-5 -4 -3 -2 -1 0 1 2 3 4 5

-5 -4 -3 -2 -1 0 1 2 3 4 5]

nb=Transpose[na];
dI=IdentityMatrix[Length[na]]//MatrixForm
jj=2*Pi(na-nb)//MatrixForm
F=Sinc[(na-nb)t]Exp[i(na-nb)Pi*t]//MatrixForm
cr=t*dI+p*(1-dI)*F;
ii=1;
For[kc=-Pi,kc<Pi,kc+=Pi/15,
T1=(kc+2*Pi*na)(kc+2*Pi*nb);
M=T1.Inverse[cr];
Nn=cr;
Nnm=M/N;
w=Eigenvalues[Nnm];
vy[[:,i]]=Sqrt(Real(w[1:t]))*(t/(2*pi));
i=i+1]


• I don't think your matrix is defined in proper Mathematica syntax...and why not just us ConstantArray[Range[-5, 5], 11] to build it? Also MatrixForm is just a display wrapper. It'll mess up all your calculations so leave it off. In general I think you should just take a day and read the documentation and figure out how Mathematica works rather than try to directly transcribe your MATLAB code and make yourself miserable. – b3m2a1 Sep 13 at 20:31