Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
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

up vote 3 down vote accepted

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.)

share|improve this answer
    
That Select method is just silly... Go Syntactic sugar! –  kale Oct 4 '12 at 0:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.