how to generate uniform discrete distribution starts from zero?

1 visualizzazione (ultimi 30 giorni)
Hi;
I want to generate a matrix that generated from uniform discrete distribution. I use unidrnd command but this command starts number from 1. For example I write:
A=unidrnd(2,5,5) and result is:
A=
1 1 1 1 1
1 2 2 1 1
2 2 1 2 1
1 2 2 2 1
1 1 1 2 2
But I need that random numbers generated from uniform discrete distribution should start from zero not 1.
Any suggestion about the problem?
Thanks in advance; Regards...

Risposta accettata

Torsten
Torsten il 17 Dic 2015
A=unidrnd(2,5,5)-1;
Best wishes
Torsten.

Più risposte (1)

Image Analyst
Image Analyst il 17 Dic 2015
To have a lower value of 0 and still keep your top value of 2, you need to pass in one more than that as the first argument to unidrnd(), and then subtract 1
>> A=unidrnd(3,5,5)-1
A =
2 2 2 1 1
2 0 2 1 1
1 0 0 2 1
1 0 2 2 2
0 0 0 0 2
You can also use randi() and specify the starting and stopping value:
A = randi([0, 2], 5, 5)

Community Treasure Hunt

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

Start Hunting!

Translated by