0

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?

AsukaMinato
  • 9,758
  • 1
  • 14
  • 40
user75376
  • 129
  • 3
  • 5
    Can you elaborate on why you insist on using For instead of Table? If you have not yet seen it, please read https://mathematica.stackexchange.com/q/134609/12 – Szabolcs Oct 24 '20 at 18:31
  • 2
    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. – Szabolcs Oct 24 '20 at 18:33

1 Answers1

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

acacia
  • 389
  • 1
  • 9
  • 2
    I think you should avoid MatrixForm here. – Michael E2 Oct 24 '20 at 18:36
  • 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. – acacia Oct 24 '20 at 18:41
  • 1
    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. – Michael E2 Oct 24 '20 at 20:01
  • 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. – acacia Oct 24 '20 at 20:15
  • 2
    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... – Michael E2 Oct 24 '20 at 20:38
  • I edited my post to reflect this new knowledge of the good and bad way to use MatrixForm. I also simplified the code to make the array with RandomInteger directly. every possible use case for OP is provided. – acacia Oct 25 '20 at 03:45