Message Boards Message Boards

0
|
9439 Views
|
10 Replies
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

Using While Loops to fill Array

Posted 10 years ago

I am trying to write code to fill out arrays for g1,g2,g3..... with a formula to fill out each gn[x] I was trying to do this with two while loops one to count through the x's and one to count through the n's.
My two questions are 1. How do you create a While loop in a While loop so after each increment of counter1, counter2 can be reset to 0? 2. How can i create a loop the changes gn[1] to count through g2[1],g3[2],.....?

Also a side question if anybody could answer it. if I have an array a[b,c] is there a way to make any value where b OR c is negative then a[b,c]=0?

POSTED BY: Stephen HIll
10 Replies

To make it easier to understand the question, can you post some pseudo-code that describes the algorithm you're trying to implement?

Can you explain why Table is not suitable for what you are trying to do?

POSTED BY: Szabolcs Horvát
Posted 10 years ago

I do not have much experience with tables or mathematica, so i am not sure what the advantage would be. pseudo-code code would look like; While[counter1<10, While[counter2<10, g("counter1")[counter2]=something; counter2++]; counter1++ and counter2=1];

POSTED BY: Stephen HIll

It looks like this is a standard application for Table. Please look up Table in the documentation. There are many examples.

For example,

g = Table[10 i+j, {i, 10}, {j, 10}]

Try to avoid a programming style where array elements are explicitly set in a loop. Often there are easier and simpler solutions in Mathematica.

POSTED BY: Szabolcs Horvát
Posted 10 years ago

Maybe a table would work better. What i am trying to do is create a recursive relationship with the initial conditions being that the first element is 1 and all negative elements are 0's. Also with Table's can you call out individual variables like g[i,j]? Just to be clear the relationship would be based on multiple tables and would have to be filled out in a certain way like g1[i,j]=g1[i-1,,j-i]+g2[i-2,,j-i] and a system of equations like that.

POSTED BY: Stephen HIll

To compute a recursive relation, the easiest way is to define a function recursively. The example which is usually given is Fibonacci:

First clear the function definition and any cached values:

Clear[fib]

These are needed to stop the recursion and avoid an infinite loop:

fib[0] = 0
fib[1] = 1

The recursion, with caching:

fib[n_] := (fib[n] = fib[n-1] + fib[n-2])

Now try fib[10] then check the definition of fib using ?fib and see how values got cached.

Notice that this implementation uses = to cache every result, thus avoiding the exponential complexity that would result from the double recursion. This technique is called memoization and you'll find a lot of information about it by googling.

Here's a two-variable example:

Clear[f]    
f[0, j_] = 1
f[i_, 0] = 1
f[i_, j_] := f[i, j] = f[i - 1, j] + f[i, j - 1]

Doing this requires some familiarity with Mathematica, and if you're a beginner, I recommend going through at least a few tutorials first:

There's also RSolve for solving recursion relations symbolically.

POSTED BY: Szabolcs Horvát
Posted 10 years ago

I know the recursive recursive realizations that i want to use. they are just dependent on other variables in my code. Thank you very much for these multiple replies by the way.

POSTED BY: Stephen HIll

I should have spelt out explicitly that using Table is not suitable for computing recursive relations because when computing the ith element of the array, it is not possible to refer back to the i-1th element.

If you decide to use arrays (they're called lists in Mathematica) to do this computation, then Do might be a little easier than While. First the array would need to be pre-allocated, as in

arr = ConstantArray[0, {10, 10}];

Then fill it up as

Do[arr[[i, j]] = ... , {i,1,10}, {j,1,10}]

Notice that arrays are indexed using [[...]], not [...].

However, what I showed you with the Fibonacci example is probably more suitable for this type of calculation than arrays. In the end it will easier to manage, but it does have a learning curve.

You also asked about returning 0 for all negative indices. This is not possible with arrays, but it is possible with functions.

The Fibonacci example would be extended by adding this:

fib[n_ /; n < 0] = 0

or alternatively

fib[n_?Negative] = 0

In Mathematica a function can have multiple definitions for multiple argument patterns (e.g. fib was defined separately for the input 0, 1, negative inputs, or arbitrary inputs). So issuing an additional definition doesn't clear the previous one. This is why it's necessary to use Clear before redefining a function.

POSTED BY: Szabolcs Horvát
Posted 10 years ago

That looks a lot closer to what i am trying to do! my two questions would still be can me ... include if statements to be my initial conditions? Second the order i would have to calculate these tables to have no unknowns in each equation is first calculate {j,1,10} with i=1 then do that for arr2,arr3,arr4,arr5...the do all of that for i=2. I understand that that is the more difficult order to them in but i need them in that order for later in the code.

POSTED BY: Stephen HIll
Posted 10 years ago

Would it be possible for you to manually list the entries for a 4 by 4 example? Perhaps something like

1,1, function or expression to use for this entry
1,2, function or expression to use for this entry
...
4,3, function or expression to use for this entry
4,4, function or expression to use for this entry

If you could do that and the pattern of what you really wanted to accomplish would be clear to someone after reading this then someone might be able to come up with a few lines of code that would do it.

If you aren't really certain that someone else who knows almost nothing about what is in your head will be able to read that list and be able to give you the right answer on the first try then maybe you could think of another way you could write down a set of directions on a piece of paper, hand it to someone who knows nothing about anything except how to follow directions, they would take that sheet of paper and come back in an hour with exactly what it is that you need done.

Without having something more concrete to work with people are groping and trying to figure out how to correctly "do some stuff with some stuff."

POSTED BY: Bill Simpson
Posted 10 years ago

I understand just i have a very big thing of code and only had a small part i wondered about. The do loop fixed the problem i was having with embedding while loops. Thank you though.

POSTED BY: Stephen HIll
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract