From: Oleg Komarov on 6 Jul 2010 12:38 "Steven Lord" <slord(a)mathworks.com> wrote in message <i0vbcl$hf1$1(a)fred.mathworks.com>... > > "Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message > news:i0v47a$oe8$1(a)fred.mathworks.com... > > "Arkadiusz Gola" <a.gola(a)pollub.pl> wrote in message > > <i0uprs$5rr$1(a)fred.mathworks.com>... > >> Hello, > >> > >> I want to generate random [0-1] m x n matrix where for example 70% are > >> "1" fields and 30% are "0" fields. Do you think is it possible? What > >> function should I use? > >> Thank you in advance. > > ========== > > > > Do you mean that there should be a 70% probability of a matrix element > > being 1? If so, do > > rand(m,n)>0.3 > > Or do you want 70% of the elements to be 1 and 30% to be 0? > > % Assuming m*n is a multiple of 100 here > % You will need to tweak this code if this assumption is not valid > m = 20; > n = 20; > v = [ones(1, 70*m*n/100), zeros(1, 30*m*n/100)]; > M = reshape(v(randperm(m*n)), m, n) > > -- > Steve Lord > slord(a)mathworks.com > comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ > To contact Technical Support use the Contact Us link on > http://www.mathworks.com > Which is de facto the solution I proposed. Oleg
From: Arkadiusz Gola on 7 Jul 2010 08:44
"Oleg Komarov" <oleg.komarovRemove.this(a)hotmail.it> wrote in message <i0vm5c$m4e$1(a)fred.mathworks.com>... > "Steven Lord" <slord(a)mathworks.com> wrote in message <i0vbcl$hf1$1(a)fred.mathworks.com>... > > > > "Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message > > news:i0v47a$oe8$1(a)fred.mathworks.com... > > > "Arkadiusz Gola" <a.gola(a)pollub.pl> wrote in message > > > <i0uprs$5rr$1(a)fred.mathworks.com>... > > >> Hello, > > >> > > >> I want to generate random [0-1] m x n matrix where for example 70% are > > >> "1" fields and 30% are "0" fields. Do you think is it possible? What > > >> function should I use? > > >> Thank you in advance. > > > ========== > > > > > > Do you mean that there should be a 70% probability of a matrix element > > > being 1? If so, do > > > rand(m,n)>0.3 > > > > Or do you want 70% of the elements to be 1 and 30% to be 0? > > > > % Assuming m*n is a multiple of 100 here > > % You will need to tweak this code if this assumption is not valid > > m = 20; > > n = 20; > > v = [ones(1, 70*m*n/100), zeros(1, 30*m*n/100)]; > > M = reshape(v(randperm(m*n)), m, n) > > > > -- > > Steve Lord > > slord(a)mathworks.com > > comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ > > To contact Technical Support use the Contact Us link on > > http://www.mathworks.com > > > Which is de facto the solution I proposed. > > Oleg Thanks a lot !!! It works :-) Arek |