0
$\begingroup$

how does Positionwork for array? In particular if i have something like this:

m = {1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0};
Position[m, 1]

i obtain

{{1}, {6}, {8}}

If i consider an array with the same elements:

Position[Arraym,1]

i receive

{}

So if i have the same elements organized in array form how could i do ? Because my aim is to find the indices of nonzero elements of an array.

$\endgroup$
  • 7
    $\begingroup$ What is the difference between the list m and an array for you? Please give an example of what Arraym could be. $\endgroup$ – C. E. Oct 6 '18 at 11:39
  • $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$ – bbgodfrey Oct 6 '18 at 13:07
  • 1
    $\begingroup$ I presume by array you mean a matrix, which Mathematica represents as a List of lists, nested as deep as the number of dimensions of the matrix. Position check only the top level of the nested lists, unless told to look deeper. Review the documentation for the third argument of Position. $\endgroup$ – bbgodfrey Oct 6 '18 at 13:12
  • $\begingroup$ @C.E. I have an array populated through a for. I post the pseudocode : $\endgroup$ – siderius Oct 6 '18 at 14:02
  • $\begingroup$ @C.E. I have an array populated through a for , i don't post it because is full of subscripts. I've found that if i do ` Position[Array[MyArray,11],0` gives the correct result on the contrary Position[MyArray,0] gives blank brackets. what you say? $\endgroup$ – siderius Oct 6 '18 at 14:18
1
$\begingroup$

Based on what you have told me, I can now guess what the problem is. Your question is the following:

list = {1, 2, 1, 3};
Position[list, 1]

returns {{1},{3}}. On the other hand,

list[1] = 1
list[2] = 2
list[3] = 1
list[4] = 2
Position[list, 1]

gives just {}, while you expected it to be the same thing. If this is the case then you have a lot to learn about Mathematica (which is OK, we all had to start somewhere), because that is not at all how it works.

Answering this question in this format is difficult because it would require me to go cover a lot of material. I recommend that you start with the following three texts instead:

If you read these then you will understand that the corresponding concept of an array in Mathematica is a list and that it is not manipulated like this:

list[1] = 1
list[2] = 2
list[3] = 1
list[4] = 2

What you would have to do is this:

list = ConstantArray[0, 4];
list[[1]] = 1;
list[[2]] = 2;
list[[3]] = 1;
list[[4]] = 2;
Position[list, 1]

This code will give you {{1}, {3}}. The assignment list[1] = 1 is also allowed but it does something else than what you think it does. (In short: list is now a function which takes as its input either 1, 2, 3, or 4 and outputs either 1 or 2.)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.