Assigning random numbers to an variable

조회 수: 5 (최근 30일)
saphir alexandre
saphir alexandre 2022년 2월 7일
답변: Benjamin Thompson 2022년 2월 7일
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
Benjamin Thompson 2022년 2월 7일
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