Write a matlab code to generate a 1 X N matrix with values in the range of 1-100 according to a normal distribution with mean= 0 and variance=1

19 views (last 30 days)
Kumar Kulala
Kumar Kulala on 18 Sep 2021
Edited: Walter Roberson on 18 Sep 2021
Can anyone help me with this please
  1 Comment
the cyclist
the cyclist on 18 Sep 2021
The question is confusing to me, especially where you say "in the range of 1-100".
If you mean that the values should be from 1-100, then how can the mean value be equal to 0?
Or do you mean that the matrix should be length N, but only elements 1-100 should be filled in? If so, then @Ravi Narasimhan's idea of using randn to fill in the first 100 elements should be straightforward.

Sign in to comment.

Answers (2)


Walter Roberson
Walter Roberson on 18 Sep 2021
Edited: Walter Roberson on 18 Sep 2021
You can create a normal distribution and then truncate it. The truncated distribution still has total probability 1, which is handled by multiplying the probability density function by enough that its integral totals 1.The modified probability density function no longer has 0 mean and 1 standard deviation, though.
You can see from the distribution that even though values from 1 to 100 are permitted, that statistically there is very little beyind 3.5
D = makedist('normal', 0, 1);
Td = truncate(D, 1, 100);
N = 10000;
r = random(Td, 1, N);
r(1:10)
ans = 1×10
1.4222 1.3387 1.9644 1.1506 1.5914 1.3055 2.1571 1.5775 1.0056 1.1884
actual_mean = mean(r)
actual_mean = 1.5260
actual_std = std(r)
actual_std = 0.4451
histogram(r)

Community Treasure Hunt

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

Start Hunting!

Translated by