Re: <Null> while building lists

• To: mathgroup at smc.vnet.net
• Subject: [mg112188] Re: <Null> while building lists
• From: Raffy <adraffy at gmail.com>
• Date: Sat, 4 Sep 2010 03:59:29 -0400 (EDT)
• References: <i5qhfr\$pio\$1@smc.vnet.net>

```On Sep 3, 3:08 am, Alessandro <alexxx.ma... at gmail.com> wrote:
> Hi group,
> I'm sure I'll bang me on my head as soon as I will Press <Post
> Message>,
> but it's since yesterday that I'm struggling with this...
>
> I fast defined this awkward function, to collect the {index,value} of
> the maxima of a list of reals (a Fourier transform):
>
> maxima[f_] := Block[{old = f[[1]], m = {}},
>   For[i = 2, i < Length[f]/2, i++,
>     If[f[[i]] > old && f[[i + 1]] < f[[i]],
>      AppendTo[m, {i, f[[i]]}]];
>     old = f[[i]]
>     ]
>    m
>   ]
>
> Problem is, what I get is e.g. this:
>
> > maxima[f]
> > {{14 Null, 10.8853 Null}, {21 Null, 3.34261 Null}, {28 Null, 1.47219 Nu=
ll}}
>
> I'm sure I do not have spaces anywhere (it happened something similar
> due to this error, time ago),
> so what's wrong?
>
> thanks for any help...
>
> alessandro

Your For[] loop returns Null which is getting multiplied by variable m
("...] m" vs "]; m").

Here's a version of your function where the search is compiled (worked
for a single vector of machine reals).

With[{f =
Compile[{{v, _Real, 1}}, Module[{max = v[[1]], cur = v[[2]], nxt},
Do[nxt = v[[i + 1]];
If[cur > max && cur > nxt, Sow@{i, cur}; max = cur];
cur = nxt, {i, 2, Quotient[Length[v], 2]}]
]]},
ClearAll[maxima];
maxima[_] = {};
maxima[v_List /; Length[v] > 2 && VectorQ[v, NumericQ]] := Join @@
Last@Reap@f[v];
];

```

• Prev by Date: Re: nonlinearfit for data with errors in both coordinates
• Next by Date: Re: <Null> while building lists
• Previous by thread: Re: <Null> while building lists
• Next by thread: Re: <Null> while building lists