MATLAB: How to create a random 10×10 matrix with values between -10 and 10

matrices

Z=rand(10,10) give me a random 10×10 matrix but between 0 and 1. How would I change this to be between -10 and 10?

Best Answer

a = -10;
b = 10;
Z = (b-a).*rand(10) + a;
command window:
>> Z
Z =
Columns 1 through 7
-7.8888 -7.3805 -5.7111 -8.3337 2.7580 4.6775 4.6755
1.8766 6.6746 0.3986 0.2221 -9.6776 -3.3602 -1.2566
-4.3454 6.0094 9.7837 -2.6633 7.9191 6.7950 -2.4032
-6.8956 8.3576 -0.2017 4.7896 0.3075 -2.5655 9.5931
-9.9868 -7.2539 3.8975 0.4948 0.8904 6.5643 -2.0201
-4.3281 0.0946 -1.7716 6.0904 2.1288 -6.4696 -1.1963
1.0162 -1.9008 -9.3045 6.3382 5.2087 -7.4096 -6.8638
7.4180 -6.5286 -4.1434 -6.2106 7.1069 7.5977 -3.4793
-9.1549 1.5037 6.0288 -7.5261 -2.3426 -9.1184 -3.7188
8.0944 2.1244 -3.0700 6.4199 -8.3070 3.7344 7.8900
Columns 8 through 10
-5.0595 4.0009 -4.8808
-3.7864 -5.7098 8.5834
-1.8226 3.5981 -0.6649
4.1602 1.1459 -4.9198
-7.1272 7.0136 -1.3756
7.4264 1.1713 4.0506
-8.3369 8.0355 -1.9534
-0.7652 -1.6096 -6.3632
-9.3922 -2.8374 7.1250
5.0640 -0.2202 1.6840
Related Question