# Getting the 557th element of a table

Make a table with elements $\sin(\sqrt{8 n}/4) + r\,$for $n$ from $1$ to $1000$, where $r$ is a random real number between $-0.1$ and $0.1$ that is different for each value of $n$. Also make a plot of all the points of this table. What is the $557^{th}$ element of this table?

Everything is working well, but I can't get the 557th element of the table. I've tried to use the `Select` command.

````Select[TableSin[Sqrt[(a*n)/4]] + Random[Real, {-0.1, 0.1}], {n, 1, 1000}] ,557]
````

but it's not working. I can't figure out how to fix it.

Can anyone give me some hints?

-
Look at `Take` in the documentation – rm -rf♦ Oct 3 '12 at 21:59
Sounds more like `Part` is what he is looking for. – Mr Alpha Oct 3 '12 at 22:01
My professor gave the hint: use "Select" – user43523 Oct 3 '12 at 22:04

3
And we gave you two more hints... `Select` would be a really terrible way to do it and I would not advise it. You can probably teach your professor something new today :) – rm -rf♦ Oct 3 '12 at 22:11

1
The problem specifies "different" random reals to be used. Even for such a small list length as 1000, so far as I'm aware there's no guarantee that Mathematica will provide distinct ones. To be extra certain (if it's important), you can take additional measures, e.g.: generate a considerably longer list of random reals; use `Union` to extract just the distinct entries; and then, if necessary, shorten that result to the 1000 you need. However, the actual intention of the problem statement may have been merely not to use the same randomly chosen number for each item. – murray Oct 4 '12 at 0:25
show 9 more comments

## 1 Answer

`Select` is wrong in this case, you can't use it to get the n-th element of a list without using additional helper functions.

You'll get the most out of this exercise by looking at `Part`, which is a very flexible function for extracting elements out of a list based on their position, for example

````x = {1,1,2,3,5,8,13,21,34,55}

(* Explicit syntax. Extracts the 3rd element out of a list
   (first element has index 1). *)
Part[x, 3]
(* ==> 2 *)

(* Syntactic sugar: using [[ ]] for Part *)
x[[3]]
(* ==> 2 *)
````

`Part` can do much more, e.g. extracting ranges, submatrices and so on. While not necessary to solve this problem, I highly recommend reading a few paragraphs in the Mathematica help - you will need it again soon. :-)

For the fun of it, here's the easiest way of doing the same task with `Select` instead of `Part` I could come up with:

````x = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55}
Select[
    MapIndexed[{#1, First@#2} &, x],
    Last[#] == 3 &
] // First // First
(* ==> 2 *)
````

(Manually index the list wasting a ton of memory, then pick the element and discard the indexing again.)

-

That `Select` method is just silly... Go Syntactic sugar! – kale Oct 4 '12 at 0:45
lang-mma
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 9, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.9050417542457581, "perplexity_flag": "middle"}