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.

  • $\begingroup$ If you want to address specific entries you should look up Part in the documentation. $\endgroup$
    – Sektor
    Mar 10, 2015 at 19:48
  • $\begingroup$ Os this what you mean? Type ?rayseg and you get a list of all the defined values. $\endgroup$
    – Jens
    Mar 10, 2015 at 19:55
  • $\begingroup$ 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. >> $\endgroup$
    – Whose
    Mar 10, 2015 at 19:56
  • $\begingroup$ 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. $\endgroup$
    – bbgodfrey
    Mar 10, 2015 at 20:00
  • 1
    $\begingroup$ 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. $\endgroup$
    – Szabolcs
    Mar 10, 2015 at 20:22

1 Answer 1


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]]] &[


(* ==> {{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.

  • $\begingroup$ ?rayseg was what I was looking for Thank you Jens $\endgroup$
    – Whose
    Mar 10, 2015 at 20:01
  • $\begingroup$ I see - but I also just posted another method. $\endgroup$
    – Jens
    Mar 10, 2015 at 20:02
  • $\begingroup$ 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? $\endgroup$
    – Whose
    Mar 10, 2015 at 20:04
  • $\begingroup$ 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 $\endgroup$
    – Whose
    Mar 10, 2015 at 20:09
  • $\begingroup$ I tried to add a quick explanation to my answer. $\endgroup$
    – Jens
    Mar 10, 2015 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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