Mathematica Asked by user75376 on December 17, 2020
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?
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.
Answered by acacia on December 17, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP