Mathematica: Position of elements in a list

In summary, the conversation discusses using the Insert function in Mathematica to add an element to a list at a specific position. The user also mentions using the Position function to find the position of a specific element in a list. There is also a discussion about finding the smallest element in a list and using it to determine a position.
  • #1
Sarah rob
16
0
If I have a list say
li = {18, 22, 4, 10}
and I want to add for example 7 to this list
Join[li, {7}] to get {18, 22, 4, 10, 7},
can I insert the 7 after the 4, to get {18, 22, 4, 7, 10},
I can't just use Sort[ {18, 22, 5, 10, 7}] as I don' t want to change the order of the whole list?
 
Physics news on Phys.org
  • #2
http://reference.wolfram.com/mathematica/ref/Insert.html

Insert[list, elem, n]
inserts elem at position n in list. If n is negative, the position is counted from the end.

You might also want to look up the help for Position (so that you can find out what is the position of e.g. 4 in your list).
 
  • #3
Thanks for that SredniVashtar with regards to position I need to make some kind of rule for n, I tried liss = 7 - li to get {-11, -15, 3, -3}

But I come up with difficulties because if I had 11 -li which gives me {-7, -11, 7, 1} I need to find the position of the smallest positive number
 
  • #4
Not sure what you are trying to accomplish.
If your list has non-negative numbers only, in the first place, why not using Min to find the smallest entry?

Position[ li , Min[li] ]

Perhaps you have to treat zeros in a special manner?
 
Last edited:
  • #5


Yes, you can insert the 7 after the 4 in the list using the Insert function in Mathematica. For example, you can write: Insert[li, 7, 4] which will give you the desired output {18, 22, 4, 7, 10}. This function allows you to specify the position where you want to insert the element, in this case, after the 4. This way, you can add elements to a specific position in a list without changing the order of the rest of the elements.
 

1. How can I find the position of a specific element in a list using Mathematica?

To find the position of an element in a list using Mathematica, you can use the "Position" function. This function takes two arguments - the list and the element you are searching for - and returns a list of all the positions where the element occurs in the list.

2. Can Mathematica find the position of multiple elements in a list?

Yes, the "Position" function can also be used to find the positions of multiple elements in a list. Instead of passing a single element as the second argument, you can pass a list of elements. The function will then return a list of lists, with each inner list containing the positions of one of the elements.

3. How do I retrieve the position of the first occurrence of an element in a list?

To retrieve the position of the first occurrence of an element in a list, you can use the "First" function. This function takes the list of positions returned by the "Position" function and returns the first element of that list, which is the position of the first occurrence of the element.

4. Is it possible to find the position of an element based on a certain condition?

Yes, you can use the "Position" function with a conditional statement to find the position of an element based on a certain condition. For example, you can use the "Position" function with the "Select" function to find the position of all elements in a list that are greater than a certain value.

5. How can I update the position of an element in a list using Mathematica?

To update the position of an element in a list using Mathematica, you can use the "ReplacePart" function. This function takes three arguments - the list, the position of the element you want to update, and the new value you want to replace it with. It then returns the updated list with the element at the specified position replaced by the new value.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Replies
2
Views
981
  • Precalculus Mathematics Homework Help
Replies
11
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Replies
7
Views
841
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Replies
0
Views
306
  • Programming and Computer Science
Replies
15
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
Back
Top