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

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

Discussion Overview

The discussion revolves around finding a more compact way to index elements from a list in Mathematica using a list of indices. Participants explore alternatives to the standard indexing method, considering comparisons with MATLAB syntax and discussing the use of specific Mathematica functions.

Discussion Character

  • Exploratory, Technical explanation, Debate/contested

Main Points Raised

  • One participant asks if there is a shortcut for indexing a list using a list of indices.
  • Another participant suggests list comprehensions but clarifies that the focus is on Mathematica's indexing.
  • A participant compares the desired functionality to MATLAB's syntax for indexing arrays with an array of indices.
  • One participant proposes using the syntax MyList[[ Sequence @@ ind ]] as a potential solution.
  • Another participant expresses interest in understanding the use of @@ in Mathematica, providing examples of its application with the Sqrt function.
  • A later reply suggests using FullForm to understand the structure of expressions involving @@.

Areas of Agreement / Disagreement

Participants do not reach a consensus on a definitive solution, and multiple approaches are discussed without agreement on the best method.

Contextual Notes

There is uncertainty regarding the behavior of the proposed methods, especially in relation to multidimensional lists and the specific outcomes of using @@.

Swamp Thing
Insights Author
Messages
1,047
Reaction score
795
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
5K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
9K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 36 ·
2
Replies
36
Views
5K
  • · 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