I have lists like {0, 1, 0, 3, 0, -t2, 0, -(1/t2), 0, 0, 0, 0} including zeros, non-zero integers, and times. I wonder how I get the position of all non-zero integers?
Asked
Active
Viewed 116 times
1
user174967
- 93
- 6
2 Answers
0
Position[{0, 0, 0, 0, 0, -t2, 0, -(1/t2), 0, 0, 0, 0}, n_ /; n =!= 0, 1] // Rest
行跑F
- 401
- 2
- 2
-
This gives the position of both integers and times. But I only want the position of integers. I just edited my post since the original example could be confusing. The lists are
{0, 1, 0, 3, 0, -t2, 0, -(1/t2), 0, 0, 0, 0}. What I want is the position of1and3. – user174967 Oct 30 '21 at 22:04
0
Position[{0, 1, 0, 3, 0, -t2, 0, -(1/t2), 0, 0, 0, 0}, n_Integer /; n != 0, 1]
This gives the following output: {{2}, {4}}
Which says that at positions 2 and 4 there are integers that are not 0. At positions 2 and 4 are the values 1 and 3.
Nate
- 725
- 3
- 14
Position[list, n_Integer /; n != 0]? – Michael E2 Oct 30 '21 at 03:351. – Michael E2 Oct 30 '21 at 03:51t2be zero? – Syed Oct 30 '21 at 05:06{0, 1, 0, 3, 0, -t2, 0, -(1/t2), 0, 0, 0, 0}. What I want is the position of1and3. – user174967 Oct 30 '21 at 22:05t2cannot be zero. These are indeterminate variables, which are saved asTimesor possiblysymbol. – user174967 Oct 30 '21 at 22:081. Thank you so much! Would you mind posting it as an answer so that I can accept it? Also, a possible explanation would be very appreciated. I am very new to Mathematica. – user174967 Oct 30 '21 at 22:22Position[expr, pattern, 1]returns the positions of the elements ofexprat level 1 that matchpattern. The patternn_Integer /; n != 0matches integers that are nonzero. The elements ofexprare scanned left to right. The named patternn_Integerwill match each integer at level 1 and assign the value to the symboln; then, in the condition (\;), the symbolninn != 0corresponds to the matched integer; ifnis nonzero, the condition evaluates toTrueand the integernis considered to match the wholepattern. – Michael E2 Oct 31 '21 at 17:02