Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #11671 > unrolled thread

Re: Transforming/expanding a list

Started byAndrzej Kozlowski <akozlowski@gmail.com>
First post2012-09-08 07:47 +0000
Last post2012-09-08 07:47 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.soft-sys.math.mathematica

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Transforming/expanding a list Andrzej Kozlowski <akozlowski@gmail.com> - 2012-09-08 07:47 +0000

#11671 — Re: Transforming/expanding a list

FromAndrzej Kozlowski <akozlowski@gmail.com>
Date2012-09-08 07:47 +0000
SubjectRe: Transforming/expanding a list
Message-ID<k2et76$km1$1@smc.vnet.net>
On 6 Sep 2012, at 10:14, Mark Coleman <markspcoleman@gmail.com> wrote:

> I'm working on some Mathematica code that will help transform a large set of lists drawn from my company's legacy database systems. A typical row of the raw data looks like
>
> {x1,NULL,NULL,x2,NULL,NULL,NULL,NULL,x3,NULL,NULL,x4,x5,NULL} ...
>
> The lists are sequences of real numbers, separating some some number of Strings which for convenicence sake we call "NULL" (as opposed to a proper Mathematica Null). The goal is to create a new list where each NULL is replaced by the real number that most recently proceeds it (note that any number of NULLs may occur before or after any real values). I would like the code to return
>
> {x1,x1,x1,x2,x2,x2,x2,x2,x3,x3,x3,x4,x5,x5}
>
>
> Thanks
>
> Mark
>

How about something like this:

h[ls_] :=
 Module[{u, g}, g[l_] /; VectorQ[l, NumberQ] := (u = Last[l];);
  g[l_] /; Last[l] == NULL := l /. NULL -> u;
  DeleteCases[Flatten[Map[g, Split[ls]]], Null]]

for example:

ls = {1, NULL, NULL, 2, NULL, NULL, NULL, NULL, 3, NULL, NULL, 4, 5,
  NULL}

h[ls]

{1,1,2,2,2,2,3,3,5}


The way the code is written, it is essential that your x's are numbers, although one could easily change it to make it work with symbols.

Andrzej Kozlowski

[toc] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web