Assigning random numbers to an variable

11 visualizzazioni (ultimi 30 giorni)
saphir alexandre
saphir alexandre il 7 Feb 2022
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

Risposte (1)

Benjamin Thompson
Benjamin Thompson il 7 Feb 2022
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.

Categorie

Scopri di più su Random Number Generation in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by