MyList[[ 1, 3, 2 ]] using ind = {1, 3, 2}

  • Mathematica
  • Thread starter Swamp Thing
  • Start date
In summary, the conversation is about finding a shortcut for indexing into a list in Mathematica. The original code was MyList[[ m, n, o ]] but the question is if there is a more compact alternative when the indices are contained in a list like lst={1,3,2}. One suggestion was to use MyList[[ Sequence @@ ind ]], which would be equivalent to MyList[[1, 3, 2]]. The conversation also mentions using @@ to apply a function to a list, as shown in the example of Sqrt @@ Sin[2]. It is suggested to use FullForm to see the underlying structure of the expression.
  • #1
Swamp Thing
Insights Author
907
572
Given a list of indices ind = {1, 3, 2} , is there a shortcut to this? :--
Code:
MyList[[ ind[[1]], ind[[2]], ind[[3]] ]]
 
Physics news on Phys.org
  • #3
Not list comprehension, but something much simpler.

It's about Mathematica, indexing into a list.

Normally, you do MyList [[ m, n, o ]] when the indices m, n, o are available in variables. But if m, n, o are contained in a list like lst={1,3,2} then is there a more compact alternative to the code in my OP?
 
  • #4
Swamp Thing said:
Normally, you do MyList [[ m, n, o ]] when the indices m, n, o are available in variables. But if m, n, o are contained in a list like lst={1,3,2} then is there a more compact alternative to the code in my OP?
In MATLAB, syntax like arr(ind) is accepted, where ind is an array of indexes of arr.

Matlab:
>> arr1 = [5 9 10 7 6 ];
>> ind = [1 3 2];
>> arr1(ind)

ans =

     5    10     9

Have you tried something like MyList [[ lst ]], a rough equivalent of the above MATLAB code?
 
  • #5
Swamp Thing said:
Given a list of indices ind = {1, 3, 2} , is there a shortcut to this? :--
Code:
MyList[[ ind[[1]], ind[[2]], ind[[3]] ]]
Code:
MyList[[ Sequence @@ ind ]]
 
  • Informative
  • Like
Likes Swamp Thing, Wrichik Basu and topsquark
  • #6
Wrichik Basu said:
Have you tried something like MyList [[ lst ]], a rough equivalent of the above MATLAB code?
If MyList is multidimensional, the above will pull out rows 1, 3 and 2. What I'm looking for is to pull out the single element MyList[[1, 3, 2]].

DrClaude said:
MyList[[ Sequence @@ ind ]]
Thank you.
I'm now experimenting with @@, which I am meeting here for the first time.
What is going on here:
Code:
Sqrt @@ Sin[2]
Output:
##\sqrt{2}##

Code:
Sqrt @@ Sin[2.0001]
Output:
0.909256
 
  • #7
Try FullForm on what you put right of @@.

Code:
FullForm[Sin[2]]

FullForm[Sin[2.0001]]
 
  • Like
Likes Swamp Thing

1. What is the purpose of using "MyList[[ 1, 3, 2 ]] using ind = {1, 3, 2}"?

This notation is used to access specific elements in a list, in this case, the elements at indices 1, 3, and 2. It allows for easy manipulation and retrieval of data within a list.

2. How does "MyList[[ 1, 3, 2 ]] using ind = {1, 3, 2}" differ from traditional list indexing?

Traditional list indexing uses numerical indices starting from 0, while this notation uses a set of indices. This allows for non-sequential or repeated indices to be used.

3. Can multiple sets of indices be used with this notation?

Yes, multiple sets of indices can be used by separating them with a comma. For example, "MyList[[1, 3], [2, 4]]" would access the elements at indices 1 and 3, and indices 2 and 4.

4. What happens if an invalid index is included in the set?

If an invalid index is included, an error will occur and the program will not be able to access the element at that index. It is important to ensure that all indices used are valid and within the range of the list.

5. Is this notation specific to a certain programming language?

No, this notation can be used in various programming languages, but the specific syntax and implementation may differ slightly. It is important to refer to the documentation of the specific language being used for accurate usage and syntax.

Similar threads

  • Programming and Computer Science
Replies
22
Views
760
  • Introductory Physics Homework Help
Replies
7
Views
846
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
3K
  • Classical Physics
Replies
15
Views
711
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
8K
  • Introductory Physics Homework Help
Replies
12
Views
201
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
Replies
7
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
22
Views
3K
Back
Top