Re: Re: simple iteration question
- To: mathgroup at smc.vnet.net
- Subject: [mg89961] Re: [mg89928] Re: simple iteration question
- From: DrMajorBob <drmajorbob at att.net>
- Date: Wed, 25 Jun 2008 06:27:33 -0400 (EDT)
- References: <g3g017$l06$1@smc.vnet.net>
- Reply-to: drmajorbob at longhorns.com
I wonder if this is closer to the OP's intent:
mat = {{3, 2, 1}, {5, 6, 1}, {10, 5, 1}};
vec = {1, 2, 3};
Catch[Fold[If[#1.#2 > 50, Throw[#1], #1 + #2] &, 0 vec,
Prepend[mat, vec]]]
{9, 10, 5}
FoldList[(Print[{#1, #2, #1.#2, #1 + #2}]; #1 + #2) &, 0 vec,
Prepend[mat, vec]]
{{0,0,0},{1,2,3},0,{1,2,3}}
{{1,2,3},{3,2,1},10,{4,4,4}}
{{4,4,4},{5,6,1},48,{9,10,5}}
{{9,10,5},{10,5,1},145,{19,15,6}}
{{0, 0, 0}, {1, 2, 3}, {4, 4, 4}, {9, 10, 5}, {19, 15, 6}}
Bobby
On Tue, 24 Jun 2008 05:17:15 -0500, sashap <pavlyk at gmail.com> wrote:
> 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 lengt=
> h
>> I have a vector, say vector={1,2,3}
>> I want to make the following iteration. Take the first component of th=
> e =
>> 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
>> ve=
> ct=
>> 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
>
>
--
DrMajorBob at longhorns.com