Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

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

Search the Archive

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

  • To: mathgroup at smc.vnet.net
  • Subject: [mg50333] Re: Is a For loop always a no-no?
  • From: Bill Rowe <readnewsciv at earthlink.net>
  • Date: Fri, 27 Aug 2004 02:57:56 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

On 8/26/04 at 6:51 AM, rob at pio-vere.com (1.156) 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];

From this last, it looks to me like you are looking for the difference between subsequent elements of the list. If so, ListConvolve[{1,-1},list] will do what you want, i.e.,

y = Table[Subscript[x, n], {n, 5}]; 
Drop[RotateLeft[y] - y, -1] == ListConvolve[{1, -1}, y]

True

So, for a matrix where you want to do this for each row simply Map ListConvolve to each row, i.e.,

ListConvolve[{1,-1},#]&/@y

where y is the matrix
--
To reply via email subtract one hundred and four


  • Prev by Date: Re: Is a For loop always a no-no?
  • Next by Date: Re: Q: Solve gives complex solution where there is a noncomplex solution how to get the last one?
  • Previous by thread: Re: Is a For loop always a no-no?
  • Next by thread: Re: Is a For loop always a no-no?