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

  • Context: Mathematica 
  • Thread starter Thread starter Swamp Thing
  • Start date Start date
Click For Summary
SUMMARY

The discussion focuses on efficiently indexing into a list in Mathematica using a list of indices, specifically with the example ind = {1, 3, 2}. The participants suggest using the syntax MyList[[Sequence @@ ind]] as a compact alternative to the traditional MyList[[ind[[1]], ind[[2]], ind[[3]]]]. This method allows for cleaner code and is comparable to MATLAB's indexing method, where arr(ind) retrieves elements based on an index array. The use of Sequence in this context is highlighted as a powerful feature in Mathematica for unpacking lists.

PREREQUISITES
  • Familiarity with Mathematica syntax and functions
  • Understanding of list indexing in programming
  • Basic knowledge of the Sequence function in Mathematica
  • Experience with MATLAB indexing for comparative analysis
NEXT STEPS
  • Explore the use of the Sequence function in Mathematica for various applications
  • Learn about list comprehensions in Python and their differences from Mathematica indexing
  • Investigate advanced indexing techniques in MATLAB for performance optimization
  • Review Mathematica's FullForm function to understand expression structures
USEFUL FOR

Mathematica users, programmers looking to optimize list indexing, and those transitioning from MATLAB to Mathematica.

Swamp Thing
Insights Author
Messages
1,047
Reaction score
780
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
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?
 
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?
 
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   Reactions: Swamp Thing, Wrichik Basu and topsquark
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
 
Try FullForm on what you put right of @@.

Code:
FullForm[Sin[2]]

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

Similar threads

  • · Replies 28 ·
Replies
28
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
9K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
1K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
12
Views
2K