Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'Searching for a function.' topicStudent Support Forum > General > Archives > "Searching for a function."

< Previous Comment | Next Comment >Help | Reply To Comment | Reply To Topic
Author Comment/Response
Bill Simpson
01/22/13 01:11am

This is stunningly slower than the previous iterative solution when given a list of 10^4 elements, but does show that Count can be forced to solve your problem.

Count[Partition[numbers//.{x___,y_,y_,z___} -> {x,y,z}, 3, 1],{x_,y_,z_}/;(x<y&&y>z || x>y&&y<z)]+1

This uses pattern matching to turn all n,n into n, which is the slow part of this, and then uses Partition and Count to find the number of "changes in direction." If you could greatly increase the speed of merging repeated elements, and that is not what DeleteDuplicates does, then this might be as fast or faster than the iterative method.

Someone wrote to me once "There are at least a dozen different ways of doing anything in Mathematica, at least several being completely incomprehensible" so there must be ten other ways of doing this.

URL: ,

Subject (listing for 'Searching for a function.')
Author Date Posted
Searching for a function. Student Konrad 01/21/13 4:23pm
Re: Searching for a function. Bill Simpson 01/21/13 7:38pm
Re: Searching for a function. Bill Simpson 01/22/13 01:11am
Re: Searching for a function. yehuda 01/22/13 04:39am
Re: Searching for a function. Student Konrad 01/22/13 3:22pm
< Previous Comment | Next Comment >Help | Reply To Comment | Reply To Topic