0
$\begingroup$

I have currently created the following matrix:

m={{RandomReal[{0,1}],RandomReal[{0,1}]},{RandomReal[{0,1}],RandomReal[{0,1}]}} 

In other words, a $2 \times 2$ matrix where the entries are real numbers. However, when I try to set $m$ to be a variable (to use it later to compute, say eigenvalues or eigenvectors), when I type m again, I obtain a different matrix than the one first obtained. I'm guessing that this is due to the fact that typing m will compute the matrix with other random reals. However, is there a way to store the matrix that is first obtained?

Say if m outputs the matrix {{1,2},{3,4}}, how can I fix this matrix for later use without obtaining a new one? Thank you for your help.

$\endgroup$

closed as off-topic by ciao, MarcoB, Bob Hanlon, m_goldberg, Mr.Wizard Aug 9 '15 at 5:50

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – ciao, MarcoB, Bob Hanlon, m_goldberg, Mr.Wizard
If this question can be reworded to fit the rules in the help center, please edit the question.

2
$\begingroup$

As long as you use Set (m =) rather than SetDelayed (m :=) the matrix will not be given new values unless you reevaluate the definition of m.

SeedRandom[1];

Clear[m]

m = RandomReal[{0, 1}, {2, 2}]

{{0.817389, 0.11142}, {0.789526, 0.187803}}

m

{{0.817389, 0.11142}, {0.789526, 0.187803}}

m

{{0.817389, 0.11142}, {0.789526, 0.187803}}

m has a fixed value. Compare with

SeedRandom[1];

Clear[m]

m := RandomReal[{0, 1}, {2, 2}]

m

{{0.817389, 0.11142}, {0.789526, 0.187803}}

m

{{0.241361, 0.0657388}, {0.542247, 0.231155}}

$\endgroup$
  • $\begingroup$ Thank you! Are the lines of code SeedRandom[1] and Clear[m] required? Or they are just for "cleaning" purposes? $\endgroup$ – hoyast Aug 9 '15 at 4:07
  • $\begingroup$ They are not required. Using SeedRandom ensures that you can exactly repeat the same results if you reevaluate the notebook. This can help in troubleshooting with random numbers. $\endgroup$ – Bob Hanlon Aug 9 '15 at 4:18

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