MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Binary Vector Manipulation

  • To: mathgroup at
  • Subject: [mg84112] Re: Binary Vector Manipulation
  • From: thomas <thomas.muench at>
  • Date: Mon, 10 Dec 2007 20:39:28 -0500 (EST)
  • References: <fjj183$frv$>

One of several possibilities is

With[{newB = Clip[vectorA + vectorB]},
  RandomSample[Position[Normal@newB, 1], Total[newB] - Total[vectorB]]

where newB is your new vector with "more than 50%" 1's. It is achieved
by adding vectorA and vectorB, yielding some "2" entries where the "1"
entries between A and b overlapped. Clip[...] corrects for that.

In the second part you randomly replace the appropriate number of 1's
with 0's. You get this number with Total[newB] - Total[vectorB], which
is the number of excess 1's in newB. You need the "Normal@" in
"Position[Normal@newB, 1]" only if your original vectors were
SparseArrays, because Position doesn't work with SparseArrays.

another option [maybe faster, depending on the size of your vectors]:

   SparseArray@Clip[vectorA + vectorB]/.
    SparseArray[_, _, _, p_] :> Flatten@p[[2, 2]], Total[vectorB]] ->
  Table[1, {Total[vectorB]}]]

Hope that helps,

On Dec 10, 10:34 am, Tara.Ann.Lor... at wrote:
> I am need of assistance programming the following scenario:
> I have two vectors composed of 0's and 1's.  Vector "A" has 5% 1's
> (and 95% 0's) while Vector "B" has 50% 1's and 50% 0's.
> First, I would like to change Vector B to have a "1" in every place
> that Vector A also has a "1" (in other words, I will then have more
> than 50% 1's in Vector B once this step is completed).
> Then, I would like to *randomly* return Vector B back to a 50/50
> distribution of 1's and 0's.
> I greatly appreciate any proposed programming methods.
> Thank you,
> Tara

  • Prev by Date: Re: Inset problems & Export
  • Next by Date: Implementing own data functions
  • Previous by thread: Re: Binary Vector Manipulation
  • Next by thread: Re: Re: Binary Vector Manipulation