Thread Subject: Random matrix with zeros and ones and twos

Subject: Random matrix with zeros and ones and twos

From: Tim Smith

Date: 10 Nov, 2008 18:41:03

Message: 1 of 6

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

Subject: Random matrix with zeros and ones and twos

From: David

Date: 10 Nov, 2008 19:26:02

Message: 2 of 6

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

step 1:
a=zeros(100,100);

Subject: Random matrix with zeros and ones and twos

From: Walter Roberson

Date: 10 Nov, 2008 19:57:51

Message: 3 of 6

Tim Smith wrote:
> I need to create a 100 x 100 matrix filled 80% with zeros, 10% with ones and 10% twos.
> The distribution should be random.

Should it be -exactly- 10% ones and -exactly- 10% twos? Or are those the means
and there is a probability distribution for how they occur? For example, is the
rule that for any given location, the content of the location is a uniform
random distribution, 0.8 zero, 0.1 one, 0.l two? That's a quite different
situation than saying that 10% of the 100 x 100 should be ones.

Here is what you asked for:

TheMatrix = zeros(100,100);
Nel = numel(TheMatrix);
Rindices = randperm(Nel);
N10 = floor(Nel/10);
TheMatrix(Rindices(1:N10)) = 1;
TheMatrix(Rindices(N10+1:N10+N10)) = 2;

(Yes, there are more efficient ways, but the above is easily vectorized.
In particular, you don't need to generate as many random numbers as there are
elements in the full vector -- but you do want to ensure that they are -unique-
element subscripts that you generate, and selection without replacement
implemented as "selection with replacement and reject duplicates" has an
indefinite wait time.)

--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: Random matrix with zeros and ones and twos

From: Tim Smith

Date: 10 Nov, 2008 20:22:24

Message: 4 of 6

Hey,

Thanks for the reply

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.


Tim Smith

Subject: Random matrix with zeros and ones and twos

From: Walter Roberson

Date: 10 Nov, 2008 20:51:16

Message: 5 of 6

Tim Smith wrote:

> Thanks for the reply

> 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.

In that case, use the code I gave before. If it comes out too slow, then you can
speed it up afterwards.

However, I do caution you that "the distribution should be random" is insufficiently
precise. When you speak of randomness, you need to speak of particular probability
distributions.

Consider, for example, golfers hitting onto a green. They might hit and have the
ball roll into the hole (a 0); they might need one more stroke to get into the
cup (a 1), or they might need two [or more] strokes to get into the cup (a 2).
In each case, the -exact- location their ball first touches down on the
green is "random" (e.g., affected by micro-gusts of wind), and first touching
down at any particular place does not guarantee a 0, 1, or 2 -- but the
-probability- (over the long term, over sufficient shots by decent golfers)
over the long term of the "result" being 0, 1, or 2 is not evenly distributed
across the green. You -might- land far from the cup and have the ball roll directly
in anyhow, and you -might- land a fraction of an inch from the cup and have a bad
bounce and need 2 shots to get it in. You can have a probability distribution which
is not symmetric or not smooth, and yet the distribution would still be "random".

--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: Random matrix with zeros and ones and twos

From: Jos

Date: 10 Nov, 2008 20:53:02

Message: 6 of 6

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

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com