Mathematica: Getting part of a list, lithin a loop.

In summary, to get a specific part of a list, you can use the Part function or shorthand notation in Mathematica. Loops can also be used to get parts of a list, and the Span operator can be used to get a range of elements. It is also possible to get multiple parts of a list at once, and conditional statements can be used to select specific elements based on a condition.
  • #1
teroenza
195
5
When I use the code directly below, it works (indicated by the "Out:...").
Code:
tmp2 = Flatten[{33, {1, 2 + j, 6}, 55}];(*Out:{33,1,2+j,6,55} *)

tmp2[[4]]; (*Out: 6 *)

But when I use it inside a loop, it acts like the tmp2 list is not the correct size:
Code:
For[j = 0, j <= 5, j++,
tmp2 = Flatten[{33, {1, 2 + j, 6}, 55}]
   Print[tmp2[[4]]]
]

(*Out: Part::partd: Part specification tmp2[[4]] is longer than depth of object. >>*)

This is a minimal example of a larger piece, but I don't see why it's not working.
 
Physics news on Phys.org
  • #2
It looks like you forgot a semicolon after the call to Flatten.
 
  • Like
Likes teroenza
  • #3
Sigh. You are correct, thank you very much.
 

Related to Mathematica: Getting part of a list, lithin a loop.

1. How do I get a specific part of a list in Mathematica?

To get a specific part of a list in Mathematica, you can use the Part function or the shorthand notation double brackets ([[ ]]). For example, if you have a list called myList, you can get the third element using myList[[3]].

2. Can I use a loop to get parts of a list in Mathematica?

Yes, you can use a loop to get parts of a list in Mathematica. You can use a For loop or a Do loop, and within the loop, you can use the Part function or the shorthand notation to get the desired elements.

3. How do I get a range of elements from a list in Mathematica?

To get a range of elements from a list in Mathematica, you can use the Span operator, which is represented by a colon (:). For example, if you want to get the second to fifth elements of a list called myList, you can use myList[[2;;5]].

4. Is it possible to get multiple parts of a list at once in Mathematica?

Yes, it is possible to get multiple parts of a list at once in Mathematica. You can use the Part function or the shorthand notation and specify the desired elements separated by commas. For example, if you want to get the first, third, and fifth elements of a list called myList, you can use myList[[1, 3, 5]].

5. Can I use conditional statements to get parts of a list in Mathematica?

Yes, you can use conditional statements to get parts of a list in Mathematica. You can use the Select function or the Cases function to select specific elements of a list that satisfy a certain condition. For example, you can use Select[myList, # > 5 &] to get all elements of myList that are greater than 5.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
679
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
22
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top