Generate Array of Random Values

I am trying to write a function to generate an array of random values as follows:
I have as inputs an array Dims (X by 2) and the number of samples per row Iter.
I need to create an X by Iter array where each row n in the array has uniformly distributed random values between Dims(n,1) and Dims(n,2).
I am trying to do this in an efficient way.
So far I have
Values = rand(size(Dims, 1), Iter);
for i = 1:size(Values, 1)
for j = 1:Iter
Values(i,j) = Values(i,j) * ((Dims(i,2) - Dims(i,1)) + Dims(i,1);
end
end
Is there a better way to do this?

回答(2 个)

Dims = [5 10; 10 20; 30 100];
Iter = 6;
Values = rand(size(Dims,1),Iter).*(Dims(:,2)-Dims(:,1))+Dims(:,1)
Values = 3x6
7.9777 9.1161 8.4292 7.2950 8.1792 5.7199 13.2348 16.3659 13.7126 16.5311 16.6468 17.8477 48.4766 33.9438 74.4127 84.5108 78.5795 69.8849
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
D = sort(randi([-9,9],7,2),2)
D = 7x2
-9 -7 -2 6 -4 0 -9 -7 -7 5 -7 9 -9 -8
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
V = rand(7,13);
V = V.*diff(D,1,2)+D(:,1)
V = 7x13
-8.6414 -7.8458 -8.8766 -7.9022 -8.7745 -7.6785 -7.2711 -8.2223 -8.5587 -8.1508 -7.9310 -8.6058 -7.8533 -0.4894 4.2643 2.9174 2.2213 2.5891 4.0897 1.6806 -0.2124 -1.1980 5.8553 5.3818 -1.6089 0.4423 -1.3513 -3.7748 -3.8460 -1.6601 -2.1304 -2.0749 -2.8700 -3.6019 -2.0163 -3.7580 -1.0548 -0.1917 -1.7693 -8.8450 -7.0183 -7.2684 -7.2548 -7.5254 -7.4451 -8.4325 -8.5864 -7.1123 -8.1188 -8.9025 -8.2176 -7.4825 -3.9550 -5.5765 -1.1027 -1.9687 -4.5128 -3.0736 1.1153 4.9786 3.7718 -0.5216 3.8545 0.4611 -5.0082 2.0961 -6.3508 0.0811 -4.5284 -2.3581 -3.5695 4.9573 1.0495 2.5981 6.2377 -1.2204 -1.5779 0.0114 -8.0202 -8.6069 -8.1219 -8.1449 -8.7622 -8.5805 -8.6816 -8.7140 -8.5465 -8.5408 -8.6633 -8.1803 -8.0489
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

类别

帮助中心File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品

标签

Community Treasure Hunt

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

Start Hunting!

Translated by