20x10 matrix is too small. rand() generates uniformly distributed random number, and the probability of getting exactly one is significantly less. If you increase the number of the element, you will have a good chance of getting one in rand()
For example
>> M=0+(5-0).*rand(2000,1000);
>> max(M, [], 'all')
ans =
5.0000
Checking at higher precision, even this value is less than 5
>> format long
>> max(M, [], 'all')
ans =
4.999999540115017
Getting excatly 5 is "almost impossible".
If you really want 0 as lower and 5 as upper limit, then you will need to use rescale()
>> M=0+(5-0).*rand(2000,1000);
>> M = rescale(M, 0, 5);
>> max(M, [], 'all')
ans =
5
>> min(M, [], 'all')
ans =
0
Best Answer