Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #5899 > unrolled thread

Using values from inside a matrix for a new row

Started byMichelle A <hfvballgurl33@gmail.com>
First post2011-10-26 21:50 +0000
Last post2011-10-28 09:33 +0000
Articles 4 — 4 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Using values from inside a matrix for a new row Michelle A <hfvballgurl33@gmail.com> - 2011-10-26 21:50 +0000
    Re: Using values from inside a matrix for a new row "andre.robin3" <andre.robin3@wanadoo.fr> - 2011-10-27 10:43 +0000
    Re: Using values from inside a matrix for a new row Ray Koopman <koopman@sfu.ca> - 2011-10-27 10:46 +0000
    Re: Using values from inside a matrix for a new row "Dr. Wolfgang Hintze" <weh@snafu.de> - 2011-10-28 09:33 +0000

#5899 — Using values from inside a matrix for a new row

FromMichelle A <hfvballgurl33@gmail.com>
Date2011-10-26 21:50 +0000
SubjectUsing values from inside a matrix for a new row
Message-ID<j89vb0$1st$1@smc.vnet.net>
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?

Any help is greatly appreciated.  Thank you

[toc] | [next] | [standalone]


#5936

From"andre.robin3" <andre.robin3@wanadoo.fr>
Date2011-10-27 10:43 +0000
Message-ID<j8bckr$8tk$1@smc.vnet.net>
In reply to#5899
There are many solutions, for example this 3 lines solution :

initTable = {{1, 2, 3}}
addNewLine[table_] := Append[table, Map[f, table[[-1]], {1}]]  (* see remark 
(1) *)
Nest[addNewLine, initTable, 10]

Remarks :

(1) if you prefer, you can use the equivalent syntax :
    addNewLine[table_] := Append[table, f /@ table[[-1]]]   (*more 
concise,usefull, readable when you become used to mathematica *)
    addNewLine[table_] := Append[table, f /@ First[table]]  (*closer to 
Shakespeare who doesn't use [[...]] at all ! *)

(2) Thanks to your textual explanations, I have done
a  implementation that reflect the way you are thinking, not
the way computers are working. That is precisely the way
you should use mathematica :

You are describing the problem in terms of "how I do it with Excel", 
(calculate
one line from the line above). That is one solution, not bad,  to describe 
want you want to
do. I have kept this idea in the code.

On the contrary, It is not a good idea to try to describe the problem
in terms of "how a stupid software would do it"  ("initialization", use of 
"variables i,j",
"loop", ... type declaration ...). It is useless and parasitical for the 
mind, unless you have to deal
with speed problem during the calculation, (you are far from this 
situation...).



"Michelle A" <hfvballgurl33@gmail.com> a écrit dans le message de news: 
j89vb0$1st$1@smc.vnet.net...
> 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?
>
> Any help is greatly appreciated.  Thank you
>
> 


[toc] | [prev] | [next] | [standalone]


#5941

FromRay Koopman <koopman@sfu.ca>
Date2011-10-27 10:46 +0000
Message-ID<j8bcpk$8ul$1@smc.vnet.net>
In reply to#5899
On Oct 26, 2:50 pm, Michelle A <hfvballgur...@gmail.com> 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?
>
> Any help is greatly appreciated.  Thank you

NestList[#+1&, {1, 2, 3}, 9]

{1, 2, 3}
{2, 3, 4}
{3, 4, 5}
{4, 5, 6}
{5, 6, 7}
{6, 7, 8}
{7, 8, 9}
{8, 9, 10}
{9, 10, 11}
{10, 11, 12}

[toc] | [prev] | [next] | [standalone]


#5950

From"Dr. Wolfgang Hintze" <weh@snafu.de>
Date2011-10-28 09:33 +0000
Message-ID<j8dssh$k4j$1@smc.vnet.net>
In reply to#5899
"Michelle A" <hfvballgurl33@gmail.com> schrieb im Newsbeitrag 
news:j89vb0$1st$1@smc.vnet.net...
> 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?
>
> Any help is greatly appreciated.  Thank you
>
>
It is very easy. See this


In[51]:=
data = Table[Random[Integer, {1, 10}], {3}]
Out[51]=
{3, 9, 10}
In[52]:=
data + 1
Out[52]=
{4, 10, 11}

--- Wolfgang 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web