1
$\begingroup$

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.

$\endgroup$
  • $\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
2
$\begingroup$

I'd probably use something like this injection pattern:

SeedRandom[0];
k = {k1, k2, k3, k4, k5, k6};
nrate = 6;
Hold@{k1, k2, k3, k4, k5, k6} /. Hold[v_] :>
  Do[
   Block[v,
    temp = RandomReal[1, nrate];
    v = temp;
    Print[k -> k1]
    ],
   {p, 4}]
k
(*
  {0.652468,0.63307,0.682813,0.566352,0.935202,0.976188}->0.652468
  {0.238452,0.637562,0.101098,0.645525,0.159522,0.813788}->0.238452
  {0.904785,0.640712,0.30654,0.756198,0.876688,0.0191284}->0.904785
  {0.542617,0.352371,0.8992,0.968878,0.876215,0.340281}->0.542617

  {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.

|improve this answer|||||
$\endgroup$
  • $\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
0
$\begingroup$

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.432025,0.55543,0.442879,0.147811,0.983221,0.618985}

{0.684541,0.0765643,0.430437,0.903713,0.68653,0.634845}

{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!

|improve this answer|||||
$\endgroup$
  • $\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.