       Re: Is a For loop always a no-no?

• To: mathgroup at smc.vnet.net
• Subject: [mg50327] Re: [mg50312] Is a For loop always a no-no?
• From: DrBob <drbob at bigfoot.com>
• Date: Fri, 27 Aug 2004 02:57:49 -0400 (EDT)
• References: <200408261051.GAA16390@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```I gather that each yd[[i]] is a vector? You're rotating that vector, subtracting the original vector, and then dropping the last element? If so, here's an example with seven 3-vectors:

g=Most[RotateLeft@#-#]&;
f=g/@#&;
yd=Array[x,{7,3}]

{{x[1,1],x[1,2],x[1,3]},{x[2,1],x[2,2],x[2,3]},{
x[3,1],x[3,2],x[3,3]},{x[4,1],x[4,2],x[4,3]},{
x[5,1],x[5,2],x[5,3]},{x[6,1],x[6,2],x[6,3]},{x[7,1],x[7,2],x[7,3]}}

yd=f@yd

{{-x[1,1]+x[1,2],-x[1,2]+x[1,3]},{-x[2,1]+x[2,2],-
x[2,2]+x[2,3]},{-x[3,1]+x[3,2],-x[3,2]+x[3,3]},{-x[4,1]+x[4,2],-
x[4,2]+x[4,3]},{-x[5,1]+x[5,2],-x[5,2]+x[5,3]},{-x[6,1]+x[6,2],-
x[6,2]+x[6,3]},{-x[7,1]+x[7,2],-x[7,2]+x[7,3]}}

yd=f@yd

{{x[1,1]-2 x[1,2]+x[1,3]},{x[2,1]-2 x[2,2]+x[2,3]},{
x[3,1]-2 x[3,2]+x[3,3]},{x[4,1]-2 x[4,2]+x[4,3]},{
x[5,1]-2 x[5,2]+x[5,3]},{x[6,1]-2 x[6,2]+x[6,3]},{x[7,1]-2 x[7,2]+x[7,3]}}

yd=f@yd

{{},{},{},{},{},{},{}}

Most is the same as the Drop you used; it's new in version 5.

Bobby

On Thu, 26 Aug 2004 06:51:10 -0400 (EDT), 1.156 <rob at pio-vere.com> wrote:

> I realize that many times some form of Mathematica built in array function will
> do the needed job.  Here I have a matrix containing individual data
> traces in rows y[[i]]. I want to make matrix containing the corresponding
> derivative signals in rows yd[[i]].  I get this done using the following
> For loop. Matrix yd has been initialized (it wouldn't work with out it).
>
> For[i = 1, i < n, i++, yd[[i]] = Drop[RotateLeft[y[[i]]] - y[[i]], -1]];
>
> I tried the obvious (to me):
> yd = Drop[RotateLeft[y] -y, -1];
>
> But I get garbage.  It seems the whole matrix has been flattened to a
> single list and the whole list is rotated --instead of doing it row
> by row as I need.
>
> Wizzards all: is there some slick way to do this without the For loop?
> If so, it's probably faster and sure would look better in the code.
> Suggestions appreciated as usual.
>
> Rob
>
>
>

--
DrBob at bigfoot.com
www.eclecticdreams.net

```

• Prev by Date: Re: Re: Publicon problems converting sample document to LaTeX
• Next by Date: Re: Is a For loop always a no-no?
• Previous by thread: Is a For loop always a no-no?
• Next by thread: Re: Is a For loop always a no-no?