Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Indexes in Mathematica

  1. Oct 29, 2011 #1
    Hi.
    In Mathematica I have tensor T for example
    In[]:=Dimensions[T]
    Out[]:={3,3,4}

    now i have index of some element in the List form
    index={1,2,3}

    Now I want to use the element whose index is in variable index. How can I do it?
     
  2. jcsd
  3. Oct 29, 2011 #2
    T[[Sequence @@ index]]
    or
    T[[Apply[Sequence,index]]]
     
  4. Oct 29, 2011 #3
    Thank you very much! Its the only combination I didnt try!
     
  5. Oct 29, 2011 #4
    If you need to do this operation repeatedly, you're better off using Extract:

    Code (Text):
    In[4]:= T = Array[t, {5, 5, 5}];

    In[8]:= SetOptions[TimeAv, Method -> {"MinNum", 1000000}];

    In[9]:= T[[Sequence @@ {3, 3, 4}]] // TimeAv

    During evaluation of In[9]:= Total wall time is 2.646485, total cpu time is 2.65217 and total time spent evaluating the expression is 2.65217
    During evaluation of In[9]:= The expression was evaluated 1100010 times, in blocks of 110001 runs. This yields a mean timing of 2.41104*10^-6 with a blocked standard deviation of 2.28871*10^-7.

    Out[9]= {2.41104*10^-6, t[3, 3, 4]}

    In[10]:= Extract[T, {3, 3, 4}] // TimeAv

    During evaluation of In[10]:= Total wall time is 0.614893, total cpu time is 0.616039 and total time spent evaluating the expression is 0.616039
    During evaluation of In[10]:= The expression was evaluated 1100010 times, in blocks of 110001 runs. This yields a mean timing of 5.6003*10^-7 with a blocked standard deviation of 4.65709*10^-8.

    Out[10]= {5.6003*10^-7, t[3, 3, 4]}
    The tutorial http://reference.wolfram.com/mathematica/tutorial/ManipulatingListsByTheirIndices.html is worth looking at.
     
  6. Oct 29, 2011 #5
    Thank you
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Indexes in Mathematica
  1. In mathematica (Replies: 4)

  2. Mathematica and Sums (Replies: 4)

  3. Mathematica Sums (Replies: 6)

  4. Mathematica NDSolve (Replies: 27)

Loading...