Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Random matrix with zeros and ones and twos
Date: Mon, 10 Nov 2008 20:53:02 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 24
Message-ID: <gfa6ve$1cd$1@fred.mathworks.com>
References: <7946648.1226342494201.JavaMail.jakarta@nitrogen.mathforum.org>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1226350382 1421 172.30.248.35 (10 Nov 2008 20:53:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 10 Nov 2008 20:53:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 870065
Xref: news.mathworks.com comp.soft-sys.matlab:500079


Tim Smith <imaccormick@gmail.com> wrote in message <7946648.1226342494201.JavaMail.jakarta@nitrogen.mathforum.org>...
> Hey,
> 
> I need to create a 100 x 100 matrix filled 80% with zeros, 10% with ones and 10% twos. The distribution should be random.
> 
> Could please someone help me?
> 
> cheers, 
> 
> Tim Smith

Take a look at RANDP on the File Exchange:
http://www.mathworks.com/matlabcentral/fileexchange/8891

R = randp([.8 .1 .1],100,100)-1 ;

or if exactly 8,000 of the 10,000 values should be 0 then you might consider:
R = reshape([zeros(8000,1) ; ones(1000,1) ; 2 * ones(1000,1)],100,100) ;
R(:) = R(randperm(numel(R))) ;

hth
Jos