# errors in simple for loops

Im trying to run the following code, which I have translated from Matlab. I created a for loop nested inside another for loop. mW is a matrix, and vMuTilde is a vector. i.e.

dw1 = 3; mW = {{1, 0, 1}, {1, 2, 1}, {1, 1, 1}}; vMuTilde = {1, 2, 1};


I got

set:write Tag Times is protected

so I tried adding Clear[v] and Clear[p] thinking it might be related to the fact that I use this variables again and again.

Now I still get this error, and in addition I also get

"\$RecursionLimit::reclim2: Recursion depth of 1024 exceeded during evaluation of ...".

Now it looks like I'm in an infinite loop, since I don't get to the "0" printed at the end.

Thanks a lot T

For[r=1, r<=dw1,r++,
Clear[v]
v = mW[[r]];
mu = vMuTilde[[r]];
Print[mu];
For[rr=1, rr<=r-1,rr++,
Clear[p]
p = v*mW[[rr]];
v = v - p*mW[[rr]];
mu = mu - p*vMuTilde[[rr]];
]
norm = Norma[v];
Print["dd"];
mW[[r]] = v / norm;
vMuTilde[[r]] = mu / norm;
]
Print[0]

• Kuba many thanks for the editing. Dec 5, 2015 at 15:09
• Are you sure that dw1 is less than Min[Length[mW], Length[vMuTilde]]? Dec 5, 2015 at 15:36
• Yes, I wrote dw1 = Dimensions[mW][[1]]; in the line before the loop Dec 5, 2015 at 16:01

Putting the semicolons in the right places removed the error. Also, I replaced Norma with Norm. I hope Norma was not a user defined function.

For[r = 1, r <= dw1, r++, Clear[v]; v = mW[[r]];
mu = vMuTilde[[r]];
Print[mu];
For[rr = 1, rr <= r - 1, rr++,
Clear[p];
p = v*mW[[rr]];
v = v - p*mW[[rr]];
mu = mu - p*vMuTilde[[rr]];];
norm = Norm[v];
Print["dd"];
mW[[r]] = v/norm;
vMuTilde[[r]] = mu/norm;];
Print[0]

• Thank you very much, I'm running it right now. Dec 5, 2015 at 19:48
• Thanks is not required! You can upvote/accept answers if they have solved your problem. Dec 6, 2015 at 10:28