# Using While Loops to fill Array

**URL:** <https://community.wolfram.com/t/using-while-loops-to-fill-array/2680>\
**Category:** Questions\
**Tags:** mathematica\
**Created:** [July 15, 2014, 5:43pm UTC](https://community.wolfram.com/t/using-while-loops-to-fill-array/2680 "2014-07-15T17:43:06Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjh5403](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@sjh5403](https://community.wolfram.com/u/sjh5403)\
**Post date:** [July 15, 2014, 5:43pm UTC](https://community.wolfram.com/t/using-while-loops-to-fill-array/2680/1 "2014-07-15T17:43:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/szhorvat/32/3531_2.png) [@szhorvat](https://community.wolfram.com/u/szhorvat)\
**Post date:** [July 15, 2014, 6:21pm UTC](https://community.wolfram.com/t/using-while-loops-to-fill-array/2680/2 "2014-07-15T18:21:41Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![sjh5403](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@sjh5403](https://community.wolfram.com/u/sjh5403)\
**Post date:** [July 15, 2014, 6:33pm UTC](https://community.wolfram.com/t/using-while-loops-to-fill-array/2680/3 "2014-07-15T18:33:40Z")

</div>

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];

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/szhorvat/32/3531_2.png) [@szhorvat](https://community.wolfram.com/u/szhorvat)\
**Post date:** [July 15, 2014, 6:37pm UTC](https://community.wolfram.com/t/using-while-loops-to-fill-array/2680/4 "2014-07-15T18:37:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sjh5403](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@sjh5403](https://community.wolfram.com/u/sjh5403)\
**Post date:** [July 15, 2014, 6:48pm UTC](https://community.wolfram.com/t/using-while-loops-to-fill-array/2680/5 "2014-07-15T18:48:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/szhorvat/32/3531_2.png) [@szhorvat](https://community.wolfram.com/u/szhorvat)\
**Post date:** [July 15, 2014, 7:19pm UTC](https://community.wolfram.com/t/using-while-loops-to-fill-array/2680/6 "2014-07-15T19:19:39Z")

</div>

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:

- [Expressions—Wolfram Documentation](http://reference.wolfram.com/language/tutorial/EverythingIsAnExpression.html)
- [Functions and Programs—Wolfram Documentation](http://reference.wolfram.com/language/tutorial/DefiningFunctions.html)
- [Transformation Rules and Definitions—Wolfram Documentation](http://reference.wolfram.com/language/tutorial/ImmediateAndDelayedDefinitions.html)
- [Transformation Rules and Definitions—Wolfram Documentation](http://reference.wolfram.com/language/tutorial/FunctionsThatRememberValuesTheyHaveFound.html)

There’s also `RSolve` for solving recursion relations symbolically.

---

<div class="post-metadata">

**Author:** ![sjh5403](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@sjh5403](https://community.wolfram.com/u/sjh5403)\
**Post date:** [July 15, 2014, 7:26pm UTC](https://community.wolfram.com/t/using-while-loops-to-fill-array/2680/7 "2014-07-15T19:26:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/szhorvat/32/3531_2.png) [@szhorvat](https://community.wolfram.com/u/szhorvat)\
**Post date:** [July 15, 2014, 7:31pm UTC](https://community.wolfram.com/t/using-while-loops-to-fill-array/2680/8 "2014-07-15T19:31:57Z")

</div>

I should have spelt out explicitly that using `Table` is not suitable for computing recursive relations because when computing the `i`th element of the array, it is not possible to refer back to the `i-1`th 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.

---

<div class="post-metadata">

**Author:** ![sjh5403](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@sjh5403](https://community.wolfram.com/u/sjh5403)\
**Post date:** [July 15, 2014, 7:43pm UTC](https://community.wolfram.com/t/using-while-loops-to-fill-array/2680/9 "2014-07-15T19:43:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bsimpson141421356](https://avatars.discourse-cdn.com/v4/letter/b/ac91a4/32.png) [@bsimpson141421356](https://community.wolfram.com/u/bsimpson141421356)\
**Post date:** [July 16, 2014, 12:26am UTC](https://community.wolfram.com/t/using-while-loops-to-fill-array/2680/10 "2014-07-16T00:26:35Z")

</div>

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.”

---

<div class="post-metadata">

**Author:** ![sjh5403](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@sjh5403](https://community.wolfram.com/u/sjh5403)\
**Post date:** [July 16, 2014, 2:33am UTC](https://community.wolfram.com/t/using-while-loops-to-fill-array/2680/11 "2014-07-16T02:33:44Z")

</div>

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.
