Lists
- You can use Count[list, x]
to count the number of times x appears in list.
- You can use Apply[Plus, list] to add together
all the elements of a list.
- You can determine the size of a list by using the functions Length
and Dimensions. For a linear list, the command Length
gives the number of elements in the list.
In[1]:=
Length[{l, i, s, t}]
Out[1]=
4
For a matrix, the function Length gives the number of inner lists.
In[2]:=
Length[{{n, e, s, t}, {l, i, s, t}}]
Out[2]=
2
The function Dimensions tells more about the inner lists.
This is a 2 x 4 matrix.
In[3]:=
Dimensions[{{n, e, s, t},
{l, i, s, t}}]
Out[3]=
{2, 4}
There are two inner lists, and each inner list has four elements.
- Many programs you write will involve operations that need to be
iterated several times. Nest and NestList are powerful
and convenient constructs for doing this. Try these examples.
Table[Nest[(Sqrt[1 + #])&, x, n],
{n, 1, 10}] //TraditionalForm
Table[Nest[(1/(1 + #))&, x, n], {n, 1, 10}]
//TraditionalForm
NestList[1/2(# + 3/#)&, 1, 10] //TableForm
You can read more about Nest and NestList in the Help Browser.
Return to the Mathematica tips
index.
| |