0
$\begingroup$

I have a question regarding the use of the For[] loop to generate a list of outputs from a function that generates random outputs. Essentially, creating a sample of outcomes.

The function F uses

Table[RandomChoice[{1,2,3,4},m}],{j,1,m}]

to basically create a m by m matrix whose components are 1,2,3, or 4 at random. I wish to come up with a For loop function that gives me a desired number of outcomes, say n, from this function, with m bound within a range of 1 to 10. How should I construct this For loop?

$\endgroup$
2
  • 5
    $\begingroup$ Can you elaborate on why you insist on using For instead of Table? If you have not yet seen it, please read mathematica.stackexchange.com/q/134609/12 $\endgroup$
    – Szabolcs
    Commented Oct 24, 2020 at 18:31
  • 2
    $\begingroup$ If you want n such random m by m matrices, you can use RandomChoice[{1, 2, 3, 4}, {n, m, m}], which is much simpler. $\endgroup$
    – Szabolcs
    Commented Oct 24, 2020 at 18:33

1 Answer 1

2
$\begingroup$
RandomInteger[{1, 4}, {5, 5}]

this generates a 5 by 5 array of random integers in the range {1,4}

Table[RandomInteger[{1, 4}, {5, 5}], {3}]

this generates 3 arrays

Table[RandomInteger[{1, 4}, {m, m}], {m, 4, 6}, {3}]

this generates 3 of each size starting at the 4 by 4 ending on the 6 by 6

Table[RandomInteger[{1, 4}, {m, m}], {m, 4, 6}]

this generates 1 array of each size

MapThread[Plus, Table[RandomInteger[{1, 4}, {5, 5}], {10}], 1]

this generates 10 random 5 by 5 arrays then adds the individual values in each array across all arrays to return a new 5 by 5 array that represents all the totals at each individual location. normally this is bad because every time we call it, the random numbers are different. this makes the original random data disappear immediately after the result of Plus is returned. to fix this we store the result in a variable a

a = Table[RandomInteger[{1, 4}, {5, 5}], {10}]
MapThread[Plus, a, 1]

you can surpress the output with semicolon ;

a = Table[RandomInteger[{1, 4}, {5, 5}], {10}];
MapThread[Plus, a, 1]

then later if you want to do something with a you have it there. you can make lists of lists

b = Join[b,a]

you can format the matrix for display purposes

a = Table[RandomInteger[{1, 4}, {5, 5}], {10}]
myarraytotal = MapThread[Plus, a, 1]
Thread[MatrixForm[a]]
MatrixForm[myarraytotal]

Note: this is for display purposes. you should not store the result if you expect to do more computation later.

a = Table[RandomInteger[{1, 4}, {5, 5}], {10}];
myarraytotal = MatrixForm[MapThread[Plus, a, 1]];

this is bad use of MatrixForm because it is being stored with MatrixForm as the Head of the list. also bad because output is suppressed. if nothing is displayed then you should have no good reason to use MatrixForm at all.

$\endgroup$
6
  • 2
    $\begingroup$ I think you should avoid MatrixForm here. $\endgroup$
    – Michael E2
    Commented Oct 24, 2020 at 18:36
  • $\begingroup$ you can omit that if you don't want it. you can also use Grid. I prefer Framed[Grid[]]. The question was about generating a matrix so I answered the question. $\endgroup$
    – acacia
    Commented Oct 24, 2020 at 18:41
  • 1
    $\begingroup$ I just thought there should be a warning against using MatrixForm in this way, if one wants to actually compute with the matrices that are generated. The same advice would go for Framed[Grid[]]. It's explained in the linked Q&A. $\endgroup$
    – Michael E2
    Commented Oct 24, 2020 at 20:01
  • $\begingroup$ I see your point now. Maybe Delete[yourmatrixhere,0] or Map[Delete[#,0]&, yourmatrixhere] can be useful to remove MatrixForm[] then use MatrixForm[yourlistoflists] or Map[MatrixForm, yourlistoflists] to get it back. I did not see where that is explained not to use MatrixForm in an answer but if you show me I will read it. $\endgroup$
    – acacia
    Commented Oct 24, 2020 at 20:15
  • 2
    $\begingroup$ My typical way would be to save your table in list = Table[...] and display the list with MatrixForm /@ list. In the Q&A, the OP wraps the matrix cov in MatrixForm and cannot compute with it. The answers explain the purpose of MatrixForm and how to work with it. Questions linked to that Q&A show it's a common problem: (126094), (166709), (218801), (225279)...60+ more... $\endgroup$
    – Michael E2
    Commented Oct 24, 2020 at 20:38

Your Answer

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

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