MathGroup Archive 2002

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

Search the Archive

Re: How to Properly Use Partition?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg36729] Re: How to Properly Use Partition?
  • From: Robert Knapp <rknapp at wolfram.com>
  • Date: Sat, 21 Sep 2002 02:21:53 -0400 (EDT)
  • Organization: Wolfram Research, Inc.
  • References: <amem4s$41p$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

flip wrote:
> b = 2; r = 4;
> n = "10101111"
> p = Reverse[Characters[n]]
> {"1", "1", "1", "1", "0", "1", "0", "1"}
> z = Reverse[Partition[p, r]]
> {{"0", "1", "0", "1"}, {"1", "1", "1", "1"}}  (* this is okay *)
> b = 2; r = 3;
> z = Reverse[Partition[p, r]]
> {{"1", "0", "1"}, {"1", "1", "1"}}  (* this is wrong, want output to be
> {{0,1,0},{1,0,1},{1,1,1}}*)
> 
> What I want is a function that takes a string as input, parses each
> character, partitions that to whatever length I want and Prepends zeros to
> the input as needed to create an appropriate length array.
> 
> What is the correct syntax for Partition (or is there a better way?).

z = Reverse[Partition[p, r, r, {1,1}, "0"]

pads "0" on the right before partitioning.  This doesn't completely do what you 
want because when you reverse, those digits are still on the right in the first 
element (i.e., they are appended instead of prepended.  To have them prepended, 
just add

z[[1]] = RotateRight[z[[1]], r Length[z] - Length[p]];

and z will have what you want.

Here is an example with Range (easier to see what is happening)

In[1]:= p = Range[8]; b = 2; r = 3;

In[2]:= z = Reverse[Partition[p, r, r, {1, 1}, 0]]

Out[2]= {{7, 8, 0}, {4, 5, 6}, {1, 2, 3}}

In[3]:= z[[1]] = RotateRight[z[[1]], r Length[z] - Length[p]];

In[4]:= z

Out[4]= {{0, 7, 8}, {4, 5, 6}, {1, 2, 3}}

Rob Knapp

> 
> Thanks, Flip
> 
> Remove _alpha to email.
> 
> 
> 



  • Prev by Date: List operation, LabeledListPlot, Goto Statement
  • Next by Date: Choosing between Mathematica for Windows and Linux
  • Previous by thread: How to Properly Use Partition?
  • Next by thread: Re: the meaning of a exponential plot in the negative region