       Re: Working with Lists

• To: mathgroup at smc.vnet.net
• Subject: [mg104358] Re: Working with Lists
• From: Peter Breitfeld <phbrf at t-online.de>
• Date: Thu, 29 Oct 2009 02:55:48 -0500 (EST)
• References: <hc927e\$eca\$1@smc.vnet.net>

```BenT wrote:

> I have several practical problems which I need to solve, and
> understand, preferable in a generic matter well, before needing to
> apply them specifically pertaining to:
>
> using a "generic" example:
>
> f={{1,2,},{3,4,},{5,6},{7,8}}
>
> NOTE: the use of 1 thru 8 for the values is purely arbitrary so please
> to do not apply any "simplifications" based on their present values.
>
> I know have to obtain the sum of all of the elements in each of the
> lists by using
>
> Apply[Plus,f]
>
> and from it, I can use the Part function or "[[]]" method of obtaining
> each result as needed.
>
> asum=Apply[Plus,f][]
> bsum=Apply[Plus,f][]
>
> What I further need are methods to:
>
> (1) obtain separate lists, call them a and b which contain just the
> 1st and 2nd elements of each sublist respectively

The lists of the first and second elements are:

a=f[[All,1]]
b=f[[All,2]]

>
> (2) then after further, if eny modification of either list's data (a
> copletely separate issue), I need to know the method of how to merge
> them, and replace the original data.

{a,b}//Transpose
>
> I know how to apply Rule's to transform data on a list of single
> elements, but not for "larger" lists.
>
> Consider this code:
>
> f={{0,1},{7,1},{12,2}}
>
> rd={1->1/4,2=1/2}
>
> How can I apply the rd "rule" (or any Rule) to just the second
> elements' 2nd values in the f list? And more importantly how are such
> things done generically?

Create a function which takes a pair:

rdfunc[{i_,j_}]:={i,j/.rd}

Then use MapAt to apply it to the second Element of the list

MapAt[rdfund, f, 2]

Out={{0,1},{7,1/4},{12,2}}

>
> And someone please define distinct differences between:
>
> (1) list
> (2) sequence
> (2) vector
> (2) array
> (3) matrix/table
>

Vector is a list with exactly one pair of braces
Matrix is a list of vectors of equal length

There exist no symbols named Vector or Matrix, all are List, but
Mathematica can test the structure with the functions VectorQ and MatrixQ.

Array and Table are Mathematica functions to construct Lists, Matrices...

Sequence[a,b,c,...] represents a sequence of values. One out of many
other possibilities of use is the following:

Suppose you have a Function with several parameters, e.g.
f[x_,y_,z_]:=<some calculation>

and you want it to evaluate with several sets of values

val1={1,2,3}
val2={9,-1,2}

then you can't write f[val1] because f needs three parameter but val1 is
the expression List[1,2,3] which is _one_ object for Mathematica. But
you can change the Head List to "nothing", what Sequence effectively
does; though

f[Sequence@@val1]

Hope I could help
--
_________________________________________________________________
Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de

```

• Prev by Date: Re: Mathematica7 doesn't copy/paste metafile in vector format
• Next by Date: ReplaceAll evaluation
• Previous by thread: Re: Working with Lists
• Next by thread: Distribution of state occupancies in a multistate Markov model