Mathematica Table item number in Mathematica

  • Thread starter Thread starter Barnak
  • Start date Start date
  • Tags Tags
    Mathematica Table
Click For Summary
The discussion revolves around defining tables in Mathematica where certain elements are replaced with their positions within the table. The user initially seeks a way to modify a list so that specific values are replaced by their index numbers. A solution is provided using the ReplacePart function, demonstrating how to replace elements in a list with their respective positions. The conversation also touches on creating a function that returns an incrementing integer each time it is called, which is achieved by defining a counter variable. Ultimately, the user expresses gratitude for the assistance received, indicating that the provided solutions effectively addressed their needs.
Barnak
Messages
62
Reaction score
0
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 ?
 
Physics news on Phys.org
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.
 
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 ? Let's 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:
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.
 
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:
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K