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

Table item number in Mathematica

  1. Sep 11, 2011 #1
    I need to define some "Tables" in Mathematica, in which some elements contains their own item number, like the following example below :

    A := {a, b, 3 c, d}
    B := {1, b, c, d}
    C := {a, b, c, 4, d, e}
    D := {a, 2, c, d, 5, g}

    How can I do this ?
     
  2. jcsd
  3. Sep 11, 2011 #2
    I think you might want to make your question more precise and clear.

    Is this anything like what you are looking for?

    In[1]:= ReplacePart[ToExpression[Characters["abcdef"]],2,2]

    Out[1]= {a,2,c,d,e,f}

    You should recognize that if you have already assigned a value to any of a,b,...f that this will change the result.

    In[2]:= a=7

    Out[2]= 7

    In[3]:= ReplacePart[ToExpression[Characters["abcdef"]],2,2]

    Out[3]= {7,2,c,d,e,f}

    If you can better explain what you really need then it is more likely that someone will be able to help you and more likely to provide the correct answer on the first try.
     
  4. Sep 11, 2011 #3
    Sorry if I was obscure (and sorry for my bad English).

    What I'm looking for is a way to make Mathematica tell what is the position of a given element in a table. The symbols "a", "b", "c" were just simple numbers in my examples above. For some elements, I want the element-position in the table to be printed, in the table itself.

    Say we have this Table :

    A := {123, 215, 456, 786}

    I need to change it to something ike this :

    A := {123, 2, 456, 4}

    The second and fourth elements were changed by their position-number in their table.

    EDIT : Maybe there's another way around my problem. Is there a way to print a always increasing positive integer, each time the function is called ? Lets call it ff[], then I need it to do the following in Mathematica :

    In: ff[]
    Out: 1

    In: ff[]
    Out: 2

    In: ff[]
    Out: 3

    etc.
     
    Last edited: Sep 11, 2011
  5. Sep 11, 2011 #4
    I'm sorry, but each new post makes me more confused than the last.

    Maybe something in this will provide you a clue

    In[1]:= A={123,215,456,786};A=ReplacePart[A,2,2]
    Out[2]= {123,2,456,786}
    In[3]:= A=ReplacePart[A,4,4]
    Out[3]= {123,2,456,4}

    In[4]:= ffcounter=1;ff[]:=ffcounter++;ff[]
    Out[6]= 1

    In[7]:= ff[]
    Out[7]= 2

    In[8]:= ff[]
    Out[8]= 3

    You appear to be groping around, hopefully knowing a good deal more about the problem you need to solve than you are providing here. That doesn't seem to be helping. Perhaps, just for a moment as an experiment, forget everything about Mathematica and just try to explain exactly what you are given and exactly what you then need to produce.
     
  6. Sep 11, 2011 #5
    Okay thanks, the last suggestion was THE solution to my problem ! :smile:

    Sorry if my query was all confused. I'm much too tired today, too impatient, and English isn't my native language. So I apologise for the troubles. :shy:

    Anyway, you gave me the right solution ! Thank you very much again ... :cool:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Table item number in Mathematica
  1. Mathematica: Tables (Replies: 2)

Loading...