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 Center および File ExchangeRandom Number Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by