Re: Using values from inside a matrix for a new row
- To: mathgroup at smc.vnet.net
- Subject: [mg122390] Re: Using values from inside a matrix for a new row
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Thu, 27 Oct 2011 06:30:45 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
On 10/26/11 at 5:40 PM, hfvballgurl33 at gmail.com (Michelle A) wrote:
>Hello, I am new to Mathematica and I am trying to create an array of
>values that uses info from the line above. I'm thinking of it like
>how Excel would do it but I'm not sure how to go about it in
>Mathematica. I've been getting the RecursionLimit error when I try
>to call an earlier element of the array.
>Here is what I'm thinking in a simplified form:
>Say I simply want to add a constant (1 here) to each element above.
>So I want my answer to be like this:
>1 2 3
>2 3 4
>3 4 5
>f = {{1, 2, 3}, {f[[1, 1]] + 1, f[[1, 2]] + 1, f[[1, 2]] + 1}} and
>continue to have it grow to be a 10x3 matrix. I know I'll need some
>i's and j's but I can't get it to work. Do I need to initialize an
>array or make a loop to just add one row at a time in a separate
>step?
There are a variety of ways to accomplish what you want. In
fact, it can be done in Mathematica without any explicit loops
or addressing parts of the array. I would do you simple example as:
In[13]:= NestList[{1, 1, 1} + # &, {1, 2, 3}, 2]
Out[13]= {{1, 2, 3}, {2, 3, 4}, {3, 4, 5}}
Here, I've defined a pure function (the {1,1,1}+#& part) that
simply adds {1,1,1} to whatever it is applied to. Then I use
NestList to repeatedly apply this function to the starting value
of {1,2,3}. The final argument to NestList tells how many times
to repeat the function.