I have vector k = {k1, k2, k3, k4, k5, k6}.

I would like to assign numerical values to the elements of k in each step of a Do/For loop. I am currently using the following commands which are not working.

k = {k1, k2, k3, k4, k5, k6}; nrate = 6;
Do[temp = RandomReal[1, nrate]; Evaluate@k = temp; {p,4}]

The problem is once the elements are assigned numerical values the first time, the code does not remove the numerical values and reassign new values. I tried Clear[k] and other variants of that within the Do loop. But none worked. Any help will be appreciated. Thanks.

  • $\begingroup$ What you are trying to do may be possible, but will be difficult. Consider instead assigning a list of values to k and the indexing it with Part to get each value: in other words, instead of using k1 in the rest of the code, you would use k[[1]]. This is also much easier to do programmatically. $\endgroup$
    – MarcoB
    Dec 18 '19 at 15:35

I'd probably use something like this injection pattern:

k = {k1, k2, k3, k4, k5, k6};
nrate = 6;
Hold@{k1, k2, k3, k4, k5, k6} /. Hold[v_] :>
    temp = RandomReal[1, nrate];
    v = temp;
    Print[k -> k1]
   {p, 4}]

  {k1, k2, k3, k4, k5, k6}

It pretty much skips the use of k, except one can see k behaves properly in the Print[k] statement. You could replace v = temp with Evaluate@k = temp if desired.

  • $\begingroup$ Great! This works. I have one more question. Is it possible to write the command Hold@{k1, k2, k3, k4, k5, k6} in terms of the array k alone? I will be changing the number of elements in k, (e.g., k={k1,k2,...,k30}, so writing this command in terms of k will help me to automate the code better. $\endgroup$
    – user8978
    Dec 18 '19 at 16:00
  • $\begingroup$ @user8978 You can write Hold@Evaluate@k, provided k1 etc. are undefined at that point. Or better: Hold@k /. OwnValues[k] /. Hold[v_] :> ... $\endgroup$
    – Michael E2
    Dec 18 '19 at 16:02

I am not quite sure that I have understood you correctly, but try this:

     k = {k1, k2, k3, k4, k5, k6};
     nrate = 6;
  Do[Clear[k ];
      k = RandomReal[1, nrate]; 
                    Print[k], {4}]

It prints the following

(* {0.916468,0.66767,0.723805,0.200977,0.146658,0.721668}



{0.00329059,0.670352,0.687355,0.843289,0.690302,0.73796} *)

If this is what you wanted, remove the Print statement.

I should say, however, that in Mma this task can be done easier without loops. Try this:

RandomReal[{0, 1}, {4, nrate}]

Have fun!

  • $\begingroup$ I appreciate the help. However, I was asking a slightly different question. I would like to assign the elements (e.g., k1) of k different numerical values at each step becasue I will be using k1, k2, and so on for later calculations. If I use k={k1,k2,k3,k4,k5,k6}; Do[Clear[k]; k = RandomReal[1, nrate]; Print[k1];] then the output is k1. Thus k1 is not assigned a value in this operation. I would like to do that. Thank you. $\endgroup$
    – user8978
    Dec 18 '19 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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