0% found this document useful (0 votes)
6 views3 pages

Random Matrices 17 03 2025

The document provides instructions on creating matrices in Matlab, including manual input and using functions like zeros(), ones(), and eye(). It explains how to generate random numbers, both uniformly distributed and within specified intervals, as well as random integers. Additionally, it covers resetting and restoring the state of the random number generator and creating a 3-D array of random numbers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Random Matrices 17 03 2025

The document provides instructions on creating matrices in Matlab, including manual input and using functions like zeros(), ones(), and eye(). It explains how to generate random numbers, both uniformly distributed and within specified intervals, as well as random integers. Additionally, it covers resetting and restoring the state of the random number generator and creating a 3-D array of random numbers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Creating A matrix in Matlab

 Manual input A=[ 2 3 1; 2 3 4; 4 10 11]


 Build functions: zeros( ), ones( ), eye( ).
 Random numbers, rand( ), randi( )

Matrix of Random Numbers


Generate a 5-by-5 matrix of uniformly distributed random numbers between 0 and 1.

Get
r = rand(5)
r = 5×5

0.8147 0.0975 0.1576 0.1419 0.6557


0.9058 0.2785 0.9706 0.4218 0.0357
0.1270 0.5469 0.9572 0.9157 0.8491
0.9134 0.9575 0.4854 0.7922 0.9340
0.6324 0.9649 0.8003 0.9595 0.6787

Random Numbers Within Specified Interval


Generate a 10-by-1 column vector of uniformly distributed numbers in the interval (-5,5).
You can generate n random numbers in the interval (a,b) with the formula r = a + (b-
a).*rand(n,1).

Get
a = -5;
b = 5;
n = 10;
r = a + (b-a).*rand(n,1)
r = 10×1

3.1472
4.0579
-3.7301
4.1338
1.3236
-4.0246
-2.2150
0.4688
4.5751
4.6489

Random Integers
Use the randi function (instead of rand) to generate 5 random integers from the uniform
distribution between 10 and 50.

Get
r = randi([10 50],1,5)
r = 1×5

43 47 15 47 35
Reset Random Number Generator
Save the current state of the random number generator and create a 1-by-5 vector of
random numbers.

Get
s = rng;
r = rand(1,5)
r = 1×5

0.8147 0.9058 0.1270 0.9134 0.6324


r= random, n= number, g=generate rng-> to save the current state of the
random number
Restore the state of the random number generator to s, and then create a new 1-by-5 vector
of random numbers. The values are the same as before.

Get
rng(s);
r1 = rand(1,5)
r1 = 1×5

0.8147 0.9058 0.1270 0.9134 0.6324

3-D Array of Random Numbers


Create a 3-by-2-by-3 array of random numbers.
3= three rows
2= two columns
3= 3-D array (3 – Dimensional Array)

Get
X = rand([3,2,3])
X =
X(:,:,1) =

0.8147 0.9134
0.9058 0.6324
0.1270 0.0975

X(:,:,2) =

0.2785 0.9649
0.5469 0.1576
0.9575 0.9706

X(:,:,3) =

0.9572 0.1419
0.4854 0.4218
0.8003 0.9157

You might also like