Prev: DS
Next: Do NOT use virtual memory/out of memory error?
From: Mayub on 6 Aug 2010 23:12 How can I generate random values between 2 specified values. Let us say A, B and C are 3 columns. I want to generate 9 rows with values of A between 9.63 (min) and 15.7 (max), values of B between -2.28 (min) and 7.4 (max) and values of C between 0.97 (min) and 3.52 (max) . It does not matter to me whether the min and max values are inclusive or not. Whichever is simpler would be fine. Many thanks for your help!
From: Ross W on 6 Aug 2010 23:23 "Mayub " <maahluwalia(a)hotmail.com> wrote in message <i3iiu5$p8d$1(a)fred.mathworks.com>... > How can I generate random values between 2 specified values. Let us say A, B and C are 3 columns. I want to generate 9 rows with values of A between 9.63 (min) and 15.7 (max), values of B between -2.28 (min) and 7.4 (max) and values of C between 0.97 (min) and 3.52 (max) . It does not matter to me whether the min and max values are inclusive or not. Whichever is simpler would be fine. > > > Many thanks for your help! Hi Do you want uniformly distributed random values? The rand function generates uniform random numbers between 0 and 1. rand(9) will generate 9 of them. So you could use that, and then multiply by the range (max-min), and then add the min. Have a go, and post your code if you have questions. Ross
From: Matt Fig on 6 Aug 2010 23:36 According to the help for RAND: Generate uniform values from the interval [a, b]. r = a + (b-a).*rand(100,1);
From: Mayub on 8 Aug 2010 23:42 "Matt Fig" <spamanon(a)yahoo.com> wrote in message <i3ikb5$lij$1(a)fred.mathworks.com>... > According to the help for RAND: > > Generate uniform values from the interval [a, b]. > r = a + (b-a).*rand(100,1); Thank you, I knew this formula from Excel, but did not recall it. Based on this I wrote the code below where the file min_max.txt contained the values I mentioned above - first row in it being min and second max. I think it is correct, because the output was OK, but please let me know your feedback. Many thanks again! A = rand(9,3); rowcoldata = size(A); nrows = rowcoldata(1,1); ncols = rowcoldata(1,2); MM = load('min_max.txt'); D1 = zeros(nrows, ncols); >> for i = 1:ncols D1(:,i) = A(:,i)* (MM(2, i) - MM(1, i))+ MM(1, i); % multiply the difference first, then add min end >> D = [D1]; save('data.txt','D','-ascii');
From: us on 9 Aug 2010 03:01
"Mayub " <maahluwalia(a)hotmail.com> wrote in message <i3ntec$kgo$1(a)fred.mathworks.com>... > "Matt Fig" <spamanon(a)yahoo.com> wrote in message <i3ikb5$lij$1(a)fred.mathworks.com>... > > According to the help for RAND: > > > > Generate uniform values from the interval [a, b]. > > r = a + (b-a).*rand(100,1); > > Thank you, I knew this formula from Excel, but did not recall it. Based on this I wrote the code below where the file min_max.txt contained the values I mentioned above - first row in it being min and second max. I think it is correct, because the output was OK, but please let me know your feedback. Many thanks again! > > A = rand(9,3); > rowcoldata = size(A); > nrows = rowcoldata(1,1); > ncols = rowcoldata(1,2); > > MM = load('min_max.txt'); > > D1 = zeros(nrows, ncols); > >> for i = 1:ncols > D1(:,i) = A(:,i)* (MM(2, i) - MM(1, i))+ MM(1, i); % multiply the difference first, then add min > end > >> D = [D1]; > save('data.txt','D','-ascii'); well... just a bit of a manicure 1) no need to create extra vars D/D1, simply overwrite your A in the loop unless, of course, you'd use your A in other places, which does not seem to be the case... 2) the construct D=[D1] is not necessary us |