Making random matrix in matlab

In summary, for a course, the user has been asked to make a random matrix A, 5x5 with a maximum possible value of 10 and a minimum of -10. They usually use the command A=-10+20*rand(5), but have been told to use the randn command. The user has added a condition to set values outside the desired range to the boundaries. However, there is a better way to do this by choosing an appropriate mean and standard deviation for the normal distribution used by randn. The user can refer to the documentation page for the randn function for an example.
  • #1
Dell
590
0
i am using MATLAB 2008a, for the course i am doing i have been asked to make a random matrix A, 5x5 with a maximum possible value of 10 and a minimum of -10, so what i would have usually done is

Code:
A=-10+20*rand(5)

but i have been told to use the randn command, the problem is that the randn command doesn't have a maximum value of 1, and i cannot find its maximum possible value. what i have done is made the matrix and added a condition that
if A(i,j)>10, A(i,j)=10 and the same for -10, but there must be a better way.
 
Physics news on Phys.org
  • #2
randn just gives you normally distributed random numbers. By default, I believe the distribution has a mean of 0 and a standard deviation of 1 ([itex]\sigma=1[/itex], working backwards from the example they provide on the documentation page):
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/randn.html

So, assuming you're familiar with the normal distribution (Gaussian) you just need to choose an appropriate mean value, and standard deviation such that you virtually never encounter values outside of your desired range (i.e. setting the boundaries such that they lie at a very low probability). Recall that even at 1 standard deviation ([itex]\sigma=1[/itex]), you have 68% of your distribution, and that at ([itex]\sigma=3[/itex]) you have 99.7% of your values:
http://en.wikipedia.org/wiki/Normal_distribution#Standard_deviation_and_confidence_intervals

However, you'll still need to go through and set values which lie beyond the limits to your limits (e.g. -10.5 to -10). Why? That's left as an exercise to the reader, but consider how many values you'd expect to lie outside your limits if you set your limits at the ([itex]\sigma=3[/itex]) value, and you had 1000 values.

Yes Virginia, cumulative knowledge is sometimes expected!
 
  • #3
how can i change the stadnard deviation from 1 to 3?
 
  • #4
Dell said:
how can i change the stadnard deviation from 1 to 3?

Look at the example in the documentation page for the randn function. And you still have to decide whether or not setting the standard deviation to 3 is acceptable! (As well, why 3 and 1/3 standard deviations to the boundaries?)
 

1. How do I create a random matrix in MATLAB?

To create a random matrix in MATLAB, you can use the rand function. This function generates a matrix with random values between 0 and 1. You can specify the size of the matrix by using the syntax rand(m,n) for an m-by-n matrix.

2. Can I generate a random matrix with specific values in MATLAB?

Yes, you can use the randi function to generate a random matrix with specific integer values. This function takes in two arguments: the range of values you want to generate and the size of the matrix.

3. How can I create a random matrix with normal distribution in MATLAB?

You can use the randn function to generate a random matrix with normal distribution in MATLAB. This function takes in the size of the matrix as its argument and returns a matrix with values following a standard normal distribution.

4. Is there a way to control the randomness in a random matrix in MATLAB?

Yes, you can use the rng function to set the seed for the random number generator in MATLAB. This allows you to control the randomness and reproduce the same results each time you run the code.

5. How do I create a random matrix with specific characteristics in MATLAB?

You can use the gallery function to create a random matrix with specific characteristics, such as being symmetric or having a specific eigenvalue distribution. This function takes in the type of matrix you want to create and its size as its arguments.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
887
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
944
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
Back
Top