<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238999</link>
    <title>MATLAB Central Newsreader - Random matrix with zeros and ones and twos</title>
    <description>Feed for thread: Random matrix with zeros and ones and twos</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Mon, 10 Nov 2008 18:41:03 -0500</pubDate>
      <title>Random matrix with zeros and ones and twos</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238999#610120</link>
      <author>Tim Smith</author>
      <description>Hey,&lt;br&gt;
&lt;br&gt;
I need to create a 100 x 100 matrix filled 80% with zeros, 10% with ones and 10% twos. The distribution should be random.&lt;br&gt;
&lt;br&gt;
Could please someone help me?&lt;br&gt;
&lt;br&gt;
cheers, &lt;br&gt;
&lt;br&gt;
Tim Smith</description>
    </item>
    <item>
      <pubDate>Mon, 10 Nov 2008 19:26:02 -0500</pubDate>
      <title>Re: Random matrix with zeros and ones and twos</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238999#610130</link>
      <author>David </author>
      <description>Tim Smith &amp;lt;imaccormick@gmail.com&amp;gt; wrote in message &amp;lt;7946648.1226342494201.JavaMail.jakarta@nitrogen.mathforum.org&amp;gt;...&lt;br&gt;
&amp;gt; Hey,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I need to create a 100 x 100 matrix filled 80% with zeros, 10% with ones and 10% twos. The distribution should be random.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Could please someone help me?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; cheers, &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Tim Smith&lt;br&gt;
&lt;br&gt;
step 1:&lt;br&gt;
a=zeros(100,100);</description>
    </item>
    <item>
      <pubDate>Mon, 10 Nov 2008 19:57:51 -0500</pubDate>
      <title>Re: Random matrix with zeros and ones and twos</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238999#610136</link>
      <author>Walter Roberson</author>
      <description>Tim Smith wrote:&lt;br&gt;
&amp;gt; I need to create a 100 x 100 matrix filled 80% with zeros, 10% with ones and 10% twos.&lt;br&gt;
&amp;gt; The distribution should be random.&lt;br&gt;
&lt;br&gt;
Should it be -exactly- 10% ones and -exactly- 10% twos? Or are those the means&lt;br&gt;
and there is a probability distribution for how they occur? For example, is the&lt;br&gt;
rule that for any given location, the content of the location is a uniform&lt;br&gt;
random distribution, 0.8 zero, 0.1 one, 0.l two? That's a quite different&lt;br&gt;
situation than saying that 10% of the 100 x 100 should be ones.&lt;br&gt;
&lt;br&gt;
Here is what you asked for:&lt;br&gt;
&lt;br&gt;
TheMatrix = zeros(100,100);&lt;br&gt;
Nel = numel(TheMatrix);&lt;br&gt;
Rindices = randperm(Nel);&lt;br&gt;
N10 = floor(Nel/10);&lt;br&gt;
TheMatrix(Rindices(1:N10)) = 1;&lt;br&gt;
TheMatrix(Rindices(N10+1:N10+N10)) = 2;&lt;br&gt;
&lt;br&gt;
(Yes, there are more efficient ways, but the above is easily vectorized.&lt;br&gt;
In particular, you don't need to generate as many random numbers as there are&lt;br&gt;
elements in the full vector -- but you do want to ensure that they are -unique-&lt;br&gt;
element subscripts that you generate, and selection without replacement&lt;br&gt;
implemented as &quot;selection with replacement and reject duplicates&quot; has an&lt;br&gt;
indefinite wait time.)&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
.signature note: I am now avoiding replying to unclear or ambiguous postings.&lt;br&gt;
Please review questions before posting them. Be specific. Use examples of what you mean,&lt;br&gt;
of what you don't mean. Specify boundary conditions, and data classes and value&lt;br&gt;
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?</description>
    </item>
    <item>
      <pubDate>Mon, 10 Nov 2008 20:22:24 -0500</pubDate>
      <title>Re: Random matrix with zeros and ones and twos</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238999#610146</link>
      <author>Tim Smith</author>
      <description>Hey,&lt;br&gt;
&lt;br&gt;
Thanks for the reply&lt;br&gt;
&lt;br&gt;
I need to create a 100 x 100 matrix filled a% with zeros, b% with ones and c% twos a+b+c=100%. The distribution should be random.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Tim Smith</description>
    </item>
    <item>
      <pubDate>Mon, 10 Nov 2008 20:51:16 -0500</pubDate>
      <title>Re: Random matrix with zeros and ones and twos</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238999#610153</link>
      <author>Walter Roberson</author>
      <description>Tim Smith wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt; Thanks for the reply&lt;br&gt;
&lt;br&gt;
&amp;gt; I need to create a 100 x 100 matrix filled a% with zeros, b% with ones and c%&lt;br&gt;
&amp;gt; twos a+b+c=100%. The distribution should be random.&lt;br&gt;
&lt;br&gt;
In that case, use the code I gave before. If it comes out too slow, then you can&lt;br&gt;
speed it up afterwards.&lt;br&gt;
&lt;br&gt;
However, I do caution you that &quot;the distribution should be random&quot; is insufficiently&lt;br&gt;
precise. When you speak of randomness, you need to speak of particular probability&lt;br&gt;
distributions.&lt;br&gt;
&lt;br&gt;
Consider, for example, golfers hitting onto a green. They might hit and have the&lt;br&gt;
ball roll into the hole (a 0); they might need one more stroke to get into the&lt;br&gt;
cup (a 1), or they might need two [or more] strokes to get into the cup (a 2).&lt;br&gt;
In each case, the -exact- location their ball first touches down on the&lt;br&gt;
green is &quot;random&quot; (e.g., affected by micro-gusts of wind), and first touching&lt;br&gt;
down at any particular place does not guarantee a 0, 1, or 2 -- but the&lt;br&gt;
-probability- (over the long term, over sufficient shots by decent golfers)&lt;br&gt;
over the long term of the &quot;result&quot; being 0, 1, or 2 is not evenly distributed&lt;br&gt;
across the green. You -might- land far from the cup and have the ball roll directly&lt;br&gt;
in anyhow, and you -might- land a fraction of an inch from the cup and have a bad&lt;br&gt;
bounce and need 2 shots to get it in. You can have a probability distribution which&lt;br&gt;
is not symmetric or not smooth, and yet the distribution would still be &quot;random&quot;.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
.signature note: I am now avoiding replying to unclear or ambiguous postings.&lt;br&gt;
Please review questions before posting them. Be specific. Use examples of what you mean,&lt;br&gt;
of what you don't mean. Specify boundary conditions, and data classes and value&lt;br&gt;
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?</description>
    </item>
    <item>
      <pubDate>Mon, 10 Nov 2008 20:53:02 -0500</pubDate>
      <title>Re: Random matrix with zeros and ones and twos</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238999#610155</link>
      <author>Jos </author>
      <description>Tim Smith &amp;lt;imaccormick@gmail.com&amp;gt; wrote in message &amp;lt;7946648.1226342494201.JavaMail.jakarta@nitrogen.mathforum.org&amp;gt;...&lt;br&gt;
&amp;gt; Hey,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I need to create a 100 x 100 matrix filled 80% with zeros, 10% with ones and 10% twos. The distribution should be random.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Could please someone help me?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; cheers, &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Tim Smith&lt;br&gt;
&lt;br&gt;
Take a look at RANDP on the File Exchange:&lt;br&gt;
&lt;a href=&quot;http://www.mathworks.com/matlabcentral/fileexchange/8891&quot;&gt;http://www.mathworks.com/matlabcentral/fileexchange/8891&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
R = randp([.8 .1 .1],100,100)-1 ;&lt;br&gt;
&lt;br&gt;
or if exactly 8,000 of the 10,000 values should be 0 then you might consider:&lt;br&gt;
R = reshape([zeros(8000,1) ; ones(1000,1) ; 2 * ones(1000,1)],100,100) ;&lt;br&gt;
R(:) = R(randperm(numel(R))) ;&lt;br&gt;
&lt;br&gt;
hth&lt;br&gt;
Jos</description>
    </item>
  </channel>
</rss>

