Listing all values in an array

I am trying to populate an array variable using a do loop as follows

Do[rayseg[1, i] = i, {i, 1, 5, 1}]


This does what I want it to do, however I want to be able to print the whole array. But when I call the variable rayseg I get nothing.I need to address specific entries to get usable outputs, rayseg[1,1] for example.

I am aware of Table and substituting it for the Do does the job, however I still want to know how to print off an array created like before because there are some situations where I want to have a variable with depth and I am not filling it sequentially or in any ordered way so table wont do. I then want to be able to list all values within the variable.

• If you want to address specific entries you should look up Part in the documentation. – Sektor Mar 10 '15 at 19:48
• Os this what you mean? Type ?rayseg and you get a list of all the defined values. – Jens Mar 10 '15 at 19:55
• Thanks for your reply but I am aware of Part and it is of no use, even when made into a 1 dimensional array the variable rayseg cannot be addressed properly using Part, certainly not to print the whole list ie Part[rayseg,1;;5]. And rayseg[[1]] Gives the following Part::partd: Part specification rayseg[[1]] is longer than depth of object. >> – Whose Mar 10 '15 at 19:56
• Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. – bbgodfrey Mar 10 '15 at 20:00
• What you are building here is not an array though. Are you sure you don't want a proper array instead and do you understand the limitations this brings? If you are a beginner and do not know the difference between x[1] and x[[1]], then I recommend you use the latter. – Szabolcs Mar 10 '15 at 20:22

Here is a function that converts to a matrix, assuming that the definitions were made for a symbol with arguments that are positive integers:

Do[rayseg[1, i] = i, {i, 1, 5, 1}]

functionToMatrix[functionName_Symbol] :=
Normal[SparseArray[ReleaseHold[DownValues[#] /. # -> List]]] &[
functionName]

functionToMatrix[rayseg]

(* ==> {{1, 2, 3, 4, 5}} *)


The take-home message from this question is that f[i,j] is different from f[[i,j]. The double brackets mean that f has a Head of type List and is therefore what you call a matrix. But with single brackets, the Head of f is not changed to anything else when assigning something to it. So the "storage method" is very different in the two cases. With f[1,1] = 3 you create a rule that the evaluator treats the way we expect a function to work. So it is not the same as looking up a value in a List. Really, functions are implemented in Mathematica as rules, and here in particular as rules stored in the DownValues of the symbol f. That's where I go to construct a matrix out of a function in the above definition.

• ?rayseg was what I was looking for Thank you Jens – Whose Mar 10 '15 at 20:01
• I see - but I also just posted another method. – Jens Mar 10 '15 at 20:02
• Excellent, thank you. This works also. Can you please explain to me what assigning variables in the way I am is (ie rayseg[i]=...) if it is not creating an array. What is the difference between the two? – Whose Mar 10 '15 at 20:04
• yes, it is also good, a much more versatile solution but I cant vote it up sorry :( This actually suits my needs better because I just need to quickly view the contents of a variable sometimes – Whose Mar 10 '15 at 20:09
• I tried to add a quick explanation to my answer. – Jens Mar 10 '15 at 20:13