Assigning random numbers to an variable

5 visualizaciones (últimos 30 días)
saphir alexandre
saphir alexandre el 7 de Feb. de 2022
Respondida: Benjamin Thompson el 7 de Feb. de 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

Respuestas (1)

Benjamin Thompson
Benjamin Thompson el 7 de Feb. de 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.

Categorías

Más información sobre Random Number Generation en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by