Clear Filters
Clear Filters

Variables in a matrix

2 views (last 30 days)
vincent stevenson
vincent stevenson on 4 Nov 2018
Answered: Yash Ubale on 13 Nov 2018
I am trying to generate a 60x6 matrix and have each column be assigned a variable and randomly generate a value for each space. For example the first column be one variable and have it generate a random number for each location in that column and second column do the same thing independent of the first. So far I have,
m=zeros(60,6);
F=m(1);
P=m(2);
T=m(3);
r=m(4);
t=m(5);
F=1000+rand(1)*99000;
P=100+rand(1)*900;
T=10+rand(1)*90;
r=.05+rand(1)*.04;
t=.0001+rand(1)*.0008
When I run this it doesn't generate a matrix and every number is the same.

Answers (1)

Yash Ubale
Yash Ubale on 13 Nov 2018
Hello,
The problem here is that, you are storing the columns of matrix 'm' into different variables and then making changes to those variables and not the original matrix 'm' in a way that they store only one single value. Instead, you can try executing the following code :
m = zeros(60,6);
m(:,1) = rand(60,1)*99000 + 1000;
m(:,2) = rand(60,1)*900 + 100;
m(:,3) = rand(60,1)*90 + 10;
m(:,4) = rand(60,1)*0.04 + 0.05;
m(:,5) = rand(60,1)*0.0008 + 0.0001;
'rand(m,n)' generates a randomely generated matrix of size m x n. The numbers are between 0 and 1 which are drawn from a uniform distribution.
'rand(1)' will generate only one random number and not an entire column.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!