Variables in a matrix

2 次查看(过去 30 天)
vincent stevenson
vincent stevenson 2018-11-4
回答: Yash Ubale 2018-11-13
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.

回答(1 个)

Yash Ubale
Yash Ubale 2018-11-13
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.

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by