Table item number in Mathematica

In summary, the solution to the problem of defining tables in Mathematica with elements that contain their own item number is to use the function ReplacePart and the variable ffcounter to print an always increasing positive integer each time the function is called. This allows for the desired element-position to be included in the table itself. It is important to be clear and precise when explaining a problem in order to receive the correct solution.
  • #1
Barnak
63
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
  • #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.
 
  • #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 ? 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:
  • #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.
 
  • #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:
 

Related to Table item number in Mathematica

1. What is a table item number in Mathematica?

A table item number in Mathematica refers to the position or index of an item within a table or list. It is used to access or manipulate specific elements within a table.

2. How do I find the table item number in Mathematica?

The table item number can be found by using the Position function in Mathematica. This function takes a list or table as the first argument and the element to be found as the second argument, and returns the position of the element within the list.

3. Can the table item number be negative in Mathematica?

Yes, the table item number can be negative in Mathematica. A negative index indicates counting from the end of the list. For example, list[[-1]] will return the last element in the list, list[[-2]] will return the second to last element, and so on.

4. How can I change the table item number in Mathematica?

To change the table item number, you can use the Part function in Mathematica. This function takes a list or table as the first argument and the new item number as the second argument. It will return the element at the specified position.

5. Can I use non-numeric values as table item numbers in Mathematica?

Yes, you can use non-numeric values as table item numbers in Mathematica. This is known as "associative indexing" and is commonly used when working with key-value pairs in a table or list. However, it is important to note that the non-numeric values must match exactly with the element to be accessed.

Similar threads

Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
254
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
300
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
Back
Top