1
$\begingroup$

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]
$\endgroup$
3
  • $\begingroup$ Kuba many thanks for the editing. $\endgroup$
    – tamir
    Dec 5, 2015 at 15:09
  • $\begingroup$ Are you sure that dw1 is less than Min[Length[mW], Length[vMuTilde]]? $\endgroup$ Dec 5, 2015 at 15:36
  • $\begingroup$ Yes, I wrote dw1 = Dimensions[mW][[1]]; in the line before the loop $\endgroup$
    – tamir
    Dec 5, 2015 at 16:01

1 Answer 1

2
$\begingroup$

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]
$\endgroup$
2
  • $\begingroup$ Thank you very much, I'm running it right now. $\endgroup$
    – tamir
    Dec 5, 2015 at 19:48
  • $\begingroup$ Thanks is not required! You can upvote/accept answers if they have solved your problem. $\endgroup$
    – Marvin
    Dec 6, 2015 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.