Thread Subject: how to create a row of 100 numbers of 1's and 0's?

Subject: how to create a row of 100 numbers of 1's and 0's?

From: aristotelis aristou

Date: 22 Jan, 2008 00:02:02

Message: 1 of 9

Hi
I am trying to create a neural network and I need a variable
(called Targets) that will hold 100 numbers in one row,
which will be the target numbers. These numbers need to be
either 0 or 1 and need to be done in a random order in the
row of the variable Targets.
Something like this:
0 1 0 1 0 1 0 1 1 1 0 1 0 1 1 1 0 1 0 0 0 1 1 0 1 0 0
thanks

Subject: how to create a row of 100 numbers of 1's and 0's?

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 22 Jan, 2008 00:05:32

Message: 2 of 9

In article <fn3bpq$h2t$1@fred.mathworks.com>,
aristotelis aristou <aristo.telios@mathworks.com> wrote:
>I am trying to create a neural network and I need a variable
>(called Targets) that will hold 100 numbers in one row,
>which will be the target numbers. These numbers need to be
>either 0 or 1 and need to be done in a random order in the
>row of the variable Targets.
>Something like this:
>0 1 0 1 0 1 0 1 1 1 0 1 0 1 1 1 0 1 0 0 0 1 1 0 1 0 0

Targets = double(rand(1,100) > 0.5)

Leave out the double() if it is okay for Targets to be class logical

--
   So you found your solution
   What will be your last contribution?
   -- Supertramp (Fool's Overture)

Subject: how to create a row of 100 numbers of 1's and 0's?

From: aristotelis aristou

Date: 22 Jan, 2008 14:32:02

Message: 3 of 9

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fn3c0c$4l9$1@canopus.cc.umanitoba.ca>...
> Targets = double(rand(1,100) > 0.5)
>
> Leave out the double() if it is okay for Targets to be
class logical

Hi, thanks for the reply.
I tried to understand why the '> 0.5' gives 0 or 1 but I
cant. I know that if I remove the '> 0.5' it will give me
any random values or if I say '< 0.5' it gives the same
results as with the initial example you wrote . What if I
wanted to have random sequence of -1 and 1.
i.e. -1 1 1 -1 1 -1 -1 1 -1 -1 1 1 -1 1 -1

Subject: how to create a row of 100 numbers of 1's and 0's?

From: dpb

Date: 22 Jan, 2008 14:33:27

Message: 4 of 9

aristotelis aristou wrote:
> roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
> message <fn3c0c$4l9$1@canopus.cc.umanitoba.ca>...
>> Targets = double(rand(1,100) > 0.5)
>>
>> Leave out the double() if it is okay for Targets to be
> class logical
>
> Hi, thanks for the reply.
> I tried to understand why the '> 0.5' gives 0 or 1 but I
> cant. I know that if I remove the '> 0.5' it will give me
> any random values or if I say '< 0.5' it gives the same
> results as with the initial example you wrote.

It returns the logical result of the comparison--> 1 for True, 0 for
False. Save the vector before the operation and do the comparison
visually. The change of the sign of the comparison would invert the
results, not return the same results.


> wanted to have random sequence of -1 and 1.
> i.e. -1 1 1 -1 1 -1 -1 1 -1 -1 1 1 -1 1 -1

Same thing except then substitute the -1 in place of the resulting zeros
something like x(x==0) = -1

--

Subject: how to create a row of 100 numbers of 1's and 0's?

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 22 Jan, 2008 14:38:58

Message: 5 of 9

In article <fn4up2$lmm$1@fred.mathworks.com>,
aristotelis aristou <aristo.telios@mathworks.com> wrote:
>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
>message <fn3c0c$4l9$1@canopus.cc.umanitoba.ca>...
>> Targets = double(rand(1,100) > 0.5)

>> Leave out the double() if it is okay for Targets to be
>class logical

>I tried to understand why the '> 0.5' gives 0 or 1 but I
>cant. I know that if I remove the '> 0.5' it will give me
>any random values or if I say '< 0.5' it gives the same
>results as with the initial example you wrote .

rand(1,100) produces a 1 x 100 matrix of random numbers
in the range (0,1) exclusive. For such values, there is
a 50% chance that the value will be above 1/2, so if you
compare the random value to 1/2, you will get "false"
or "true" each 50% of the time. In Matlab, false is
represented by 0, and true is represented by 1, so
the result is 1 x 100 matrix of values each with a
50% chance of being 0 or 1.

>What if I
>wanted to have random sequence of -1 and 1.
>i.e. -1 1 1 -1 1 -1 -1 1 -1 -1 1 1 -1 1 -1

Targets = 2 * (rand(1,100) > 0.5) - 1

The (rand(1,100) > 0.5) part produces 0 or 1, multiply that by 2 to
get 0 or 2, subtract 1 from that to get -1 or 1.

More generally, (maxval - minval) * (rand(1,100) > 0.5) + minval
--
   "Beware of bugs in the above code; I have only proved it correct,
   not tried it." -- Donald Knuth

Subject: how to create a row of 100 numbers of 1's and 0's?

From: Anh Huy Phan

Date: 22 Jan, 2008 15:12:04

Message: 6 of 9

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fn4v62$5oi$1@canopus.cc.umanitoba.ca>...
> In article <fn4up2$lmm$1@fred.mathworks.com>,
> aristotelis aristou <aristo.telios@mathworks.com> wrote:
> >roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
> >message <fn3c0c$4l9$1@canopus.cc.umanitoba.ca>...
> >> Targets = double(rand(1,100) > 0.5)
>
> >> Leave out the double() if it is okay for Targets to be
> >class logical
>
> >I tried to understand why the '> 0.5' gives 0 or 1 but I
> >cant. I know that if I remove the '> 0.5' it will give me
> >any random values or if I say '< 0.5' it gives the same
> >results as with the initial example you wrote .
>
> rand(1,100) produces a 1 x 100 matrix of random numbers
> in the range (0,1) exclusive. For such values, there is
> a 50% chance that the value will be above 1/2, so if you
> compare the random value to 1/2, you will get "false"
> or "true" each 50% of the time. In Matlab, false is
> represented by 0, and true is represented by 1, so
> the result is 1 x 100 matrix of values each with a
> 50% chance of being 0 or 1.
>
> >What if I
> >wanted to have random sequence of -1 and 1.
> >i.e. -1 1 1 -1 1 -1 -1 1 -1 -1 1 1 -1 1 -1
>
> Targets = 2 * (rand(1,100) > 0.5) - 1
>
> The (rand(1,100) > 0.5) part produces 0 or 1, multiply
that by 2 to
> get 0 or 2, subtract 1 from that to get -1 or 1.
>
> More generally, (maxval - minval) * (rand(1,100) > 0.5)
+ minval
> --
> "Beware of bugs in the above code; I have only proved
it correct,
> not tried it." --
Donald Knuth


Let try some other functions
    randint
    randsrc

Example
  randsrc(1,10)

Anh Huy Phan
RIKEN - BSI

Subject: how to create a row of 100 numbers of 1's and 0's?

From: aristotelis aristou

Date: 22 Jan, 2008 22:38:02

Message: 7 of 9

thanks for the help guys

Subject: how to create a row of 100 numbers of 1's and 0's?

From: Greg Heath

Date: 24 Jan, 2008 11:40:47

Message: 8 of 9

On Jan 22, 5:38=A0pm, "aristotelis aristou"
<aristo.tel...@mathworks.com> wrote:
> thanks for the help guys

round(rand(1,100))

Greg

Subject: how to create a row of 100 numbers of 1's and 0's?

From: aristotelis aristou

Date: 24 Jan, 2008 16:19:03

Message: 9 of 9

Greg Heath <heath@alumni.brown.edu> wrote in message
<81e5d0f9-35ec-40bc-9d39-03ba4d63be6a@v46g2000hsv.googlegroups.com>...
> On Jan 22, 5:38=A0pm, "aristotelis aristou"
> <aristo.tel...@mathworks.com> wrote:
> > thanks for the help guys
>
> round(rand(1,100))
>
> Greg

nice and clean solution
thanks

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
random sequence numbers 1 1 aristotelis aristou 22 Jan, 2008 09:34:58
create numbers randomly aristotelis aristou 21 Jan, 2008 19:05:02
rssFeed for this Thread

Public Submission Policy

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 Disclaimer prior to use.

Contact us at files@mathworks.com