From: Arkadiusz Gola on
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.

Arek :-)
From: Peter Riddersholm on
"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.
>
> Arek :-)

This is not a very subtle way to do it, but it works:

A = zeros(4,8);

for i = 1:4
for j = 1:8
A(i,j) = rand;
if A(i,j) > 0.3; A(i,j) = 1;
else A(i,j) = 0;
end
end
end
From: Oleg Komarov on
"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.
>
> Arek :-)
% Create the number of ones and zeros to respect the percentages
A = [ones(70,1); zeros(30,1)];
% Generate a random index
idx = randperm(100);
% Select randomly and reshape
A = reshape(A(idx),[10,10]);

You can also use seqorg to retrieve a truly random idx sequence:
idx = seqorg([1,100]); % on FEX

You can find seqorg on: http://www.mathworks.com/matlabcentral/fileexchange/27942
You need an internet connection to use it.

Oleg
From: Matt J on
"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
From: Steven Lord on

"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