Re: Loop programming; how do i do this ???

• To: mathgroup at smc.vnet.net
• Subject: [mg96122] Re: [mg96071] Loop programming; how do i do this ???
• From: "David Park" <djmpark at comcast.net>
• Date: Thu, 5 Feb 2009 04:42:10 -0500 (EST)
• References: <2319870.1233744893305.JavaMail.root@m02>

```If you are new to Mathematica this might be a little difficult. If you are
new to programming it will be less difficult. What you want to use here are
Mathematica's functional programming capabilities. You can find them in
Documentation Center > Core Language > Functional Programming > (Function
and Outer, for this problem).

First, in Mathematica symbol names must begin with a letter, preferably a
small case letter so as never to conflict with built-in symbols. So we

intarray = {2, 4, 5};
conarray = {{a1, b1, c1}, {a2, b2, c2}, {a3, b3, c3}, {a4, b4,
c4}, {a5, b5, c5}};

Then, if I understand your problem correctly, you wish to take all
combinations of the second and fourth rows of the conarray with the intarray
and apply some test to them. The Mathematica command that generates all
combinations is Outer. So we can generate all these results with the
statement (Look up Outer):

(results =
Outer[f[#1, #2, #3] &, conarray[[2]], conarray[[4]],
intarray]) // MatrixForm

f[#1, #2, #3] & is a pure function, which you can find out about by looking
up Function in Help. It would be replaced by the actual test you wanted to
use. For example if you wanted to test if the second row constant was
greater than the integer times the fourth row constant you could write:

If[#1 > #3 #2]& and you would obtain an array of True and False values.

David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/

From: plzhlp [mailto:naso at live.be]

I need to implement a function into my notebook but am having great
difficulties even getting started (i'm new to mathematica and programming in
general)

The function accepts a 1d array (of variable length) of integers

which will refer to a 2d array of constants, of which all need to be tested
against eachother.

for example:

1DArrayVar = {2, 4, 5}

2DArrayCon = {
{a1,b1,c1},
{a2,b2,c2},
{a3,b3,c3},
{a4,b4,c4},
{a5,b5,c5}
}

Function[1DArrayVar]

in this scenario will test:

Do[
Do[
Do[

TEST[2DArrayCon[[2,x]],2DArrayCon[[4,y]],2DArrayCon[[5,z]]]

, {x, Length[DArrayCon[[2]]]
, {y, Length[DArrayCon[[4]]]
, {z, Length[DArrayCon[[5]]]

So basically the problem is that i need a variable amount of nested loops,
as the length of 1DArrayVar will always be unknown.

>From some research on the internet i've learned a recursive algorithm might
be the solution, but i am completely lost on how to start --

any tips would be really appreciated !

```

• Prev by Date: weighted averages from 2 matrices
• Next by Date: Re: Loop programming; how do i do this ???
• Previous by thread: Re: Loop programming; how do i do this ???
• Next by thread: Re: Loop programming; how do i do this ???