Assigning random numbers to an variable

12 次查看(过去 30 天)
Hello,
I was wondering how to assign random numbers to a varibale that already has numbers. Any help is appreciated
Example: variableX = (1:1:336);
variableY = Assigns a random number to each values of the variableX
Thank you

回答(1 个)

Benjamin Thompson
The rand function is for uniformly distributed random numbers, while randn is for normally distributed random numbers. Both functions accept arguments for the size of the output that you want:
>> A = rand(3,3)
A =
0.8147 0.9134 0.2785
0.9058 0.6324 0.5469
0.1270 0.0975 0.9575
>> B = randn(3,2)
B =
2.7694 0.7254
-1.3499 -0.0631
3.0349 0.7147
You can also overwrite a variable that already exists, so it sounds like you want:
variableX = randn(336,1);
This would overwrite what was in variableX with a new column vector of normally distributed random numbers, where the length of the vector is 336.

类别

Help CenterFile Exchange 中查找有关 Random Number Generation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by