フィルターのクリア

how to get the same result using rand

1 回表示 (過去 30 日間)
Damien
Damien 2022 年 11 月 3 日
コメント済み: Les Beckham 2022 年 11 月 3 日
im looking to answer this question: create N = 2 random vectors and each vector with L=10^6 samples
that are uniformly distributed between [-0.5 and 0.5], Plot the histogram with 40
bins of the sum of these two vectors..
so far I am here but i when i check my random numbers it ranges from 0,1 and not (-.5, .5). Can someone else me understand what I am doing wrong.
L=10^6;
a=[-.5 5];
b=[-.5 5];
a=rand(1,L);
b=rand(1,L);
sum=(a+b);
histogram(sum,40)
title('Histogram with 40 bins for sum of the 2 vectors');

回答 (2 件)

Les Beckham
Les Beckham 2022 年 11 月 3 日
編集済み: Les Beckham 2022 年 11 月 3 日
The documentation for rand states that it returns values "drawn from the uniform distribution in the interval (0,1)". If you want numbers between -0.5 and +0.5 simply subtract 0.5 from your a and b vectors.
Also, delete these two lines from your code. The don't do anything as you are redefining a and b in the next two lines:
a=[-.5 5];
b=[-.5 5];
  3 件のコメント
Damien
Damien 2022 年 11 月 3 日
Thank you for your help!
Les Beckham
Les Beckham 2022 年 11 月 3 日
You are quite welcome.
It is considered courteous in this forum to click "Accept this Answer" on the answer that best answers your question.

サインインしてコメントする。


David Hill
David Hill 2022 年 11 月 3 日
s=sum(rand(2,1e6)-.5);
histogram(s,40)

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by