MathGroup Archive 2008

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

Search the Archive

Re: simple iteration question-thanks

  • To: mathgroup at smc.vnet.net
  • Subject: [mg90048] Re: simple iteration question-thanks
  • From: dh <dh at metrohm.ch>
  • Date: Fri, 27 Jun 2008 06:17:12 -0400 (EDT)
  • References: <g3vkjm$k6o$1@smc.vnet.net>


Hi Francisco,

your problem has the smell of Fold, but we need to break and return the 

result. Breaking is easy, we may e.g. wrap the "Fold" with a "Do" and 

use Break. There is an  undocumentated feature: Break[] can take an 

argument. We can use this to return the result:

vec={1,2,3};

list={{5,6,1},{10,5,1}};

fun[x1_,x2_]:=If[x1.x2-50>=0,Break[x1],x1+x2];

Do[Fold[fun,vec,list],{1}]

hope this helps, Daniel



Francisco Gutierrez wrote:

> Many thanks to Don, Jean Marc, and Oleksandr. Their ideas were very useful 

> to solve my problem. Indeed, it implied using Fors and Ifs. I wonder if it 

> is possible to express all this functionally and not procedurally; I couldn't find a way to do it.

> Best,

> Fg

> 

> --- On Tue, 6/24/08, sashap <pavlyk at gmail.com> wrote:

> 

> From: sashap <pavlyk at gmail.com>

> Subject:  Re: simple iteration question

> Date: Tuesday, June 24, 2008, 5:17 AM

> 

> On Jun 20, 5:16 am, Francisco Gutierrez <fgutiers2... at yahoo.com>

> wrote:

>> Dear Friends:

>> I have a list of the form list={{3,2,1},{5,6,1},{10,5,1}}, of any length

>> I have a vector, say vector={1,2,3}

>> I want to make the following iteration.  Take the first component of the 

>> list, make the dot product of it and the vector, and substract a number ( sa

>  > y 50). If the result is bigger or equal than zero, stop and return the vect

>  > or. If it is less than zero, then change the vector in the followig way: =

> =

> ve=

>> ctor+list[[2]], and repeat the test.

>> Iterate until the result is equal or bigger than zero, or until the list =

> =

> en=

>> ds.

>> I haven't found a neat way of doing this. Any help is welcome

>> Francisco

>>

> 

> Hey Francisco,

> 

> here is a short way of doing it:

> 

> In[59]:= mat = {{3, 2, 1}, {5, 6, 1}, {10, 5, 1}};

> vec = {1, 2, 3};

> 

> In[61]:= Catch[

>  Fold[If[#1.#2 > 50, Throw[#1], #1 + mat[[2]]] &, vec, mat]]

> 

> Out[61]= {6, 8, 4}

> 

> Oleksandr Pavlyk=0A=0A=0A      

> 





-- 



Daniel Huber

Metrohm Ltd.

Oberdorfstr. 68

CH-9100 Herisau

Tel. +41 71 353 8585, Fax +41 71 353 8907

E-Mail:<mailto:dh at metrohm.com>

Internet:<http://www.metrohm.com>




  • Prev by Date: Re: Re: Ploting integral curves
  • Next by Date: Re: Problem with NMaximize
  • Previous by thread: Re: Re: simple iteration question-thanks
  • Next by thread: Re: RE: documentation specification for Documentation Center