3
$\begingroup$

I have a list of lists from which I would like to pick out the positions of the number 1 and then print out these positions for each sublist.

I am having some trouble collecting what I am printing into a list / matrix / array though. Below is my list of lists and the commands by which I find the positions of the 1's and print them out. Thanks.

ZeroCrossings = {{0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 1,
   0, 0, 1, 0, 0, 0, 1, 1, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1,
   0}, {0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0}, {0, 0, 0, 1, 0, 0, 
  0, 0, 1, 0, 0, 1, 1, 0}, {0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0}}

Do[Print[Flatten[Transpose[Position[ZeroCrossings[[t]], 1]]]], {t, 1,Length[ZeroCrossings]}]

{3,9}

{4,5,8,12,13}

{4,12,13}

{4,8,11,13}

{4,9,12,13}

{5,6,11}

$\endgroup$
1
  • 1
    $\begingroup$ Drop the Print and change Do to Table. Is this what you need? $\endgroup$
    – Szabolcs
    Jul 22, 2014 at 4:11

9 Answers 9

5
$\begingroup$

Primarily you just need to use Table instead of Do and Print. Also you can simplify the code:

Table[Flatten @ Position[t, 1], {t, ZeroCrossings}]
{{3, 9}, {4, 5, 8, 12, 13}, {4, 12, 13}, {4, 8, 11, 13}, {4, 9, 12, 13}, {5, 6, 11}}

See Case #2 of: Alternatives to procedural loops and iterating over lists in Mathematica

It may be simpler to use Map:

Flatten @ Position[#, 1] & /@ ZeroCrossings

This is probably a bit advanced for you right now but you could also use:

GatherBy[Position[ZeroCrossings, 1], First][[All, All, 2]]

Or if you are using Mathematica 10:

GroupBy[Position[ZeroCrossings, 1], First -> Last] // Values
$\endgroup$
1
  • $\begingroup$ Like you said first, I used Table in place of Do to solve my problem. Table[Flatten[Transpose[Position[ZeroCrossings[[t]], 1]]], {t, 1, Length[ZeroCrossings]}] Thanks for your help and advice! $\endgroup$
    – CJ B
    Jul 22, 2014 at 4:16
5
$\begingroup$

Another approach:

ZeroCrossings = {{0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, {0, 0, 0,
     1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 
    0, 1, 1, 0}, {0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0}, {0, 0, 0,
     1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0}, {0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 
    1, 0, 0, 0}};

Map[Last, GatherBy[Position[ZeroCrossings, 1], First], {-2}]

{{3, 9}, {4, 5, 8, 12, 13}, {4, 12, 13}, {4, 8, 11, 13}, {4, 9, 12,
13}, {5, 6, 11}}

$\endgroup$
4
$\begingroup$

You can also use PositionIndex in Version 10

(PositionIndex /@ ZeroCrossings)[[All, 2]]

Gives:

{{3, 9}, {4, 5, 8, 12, 13}, {4, 12, 13}, {4, 8, 11, 13}, {4, 9, 12, 13}, {5, 6, 11}}
$\endgroup$
4
$\begingroup$
zeroCrossings = {{0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, {0, 0, 0,
     1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 
    0, 1, 1, 0}, {0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0}, {0, 0, 0,
     1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0}, {0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 
    1, 0, 0, 0}};

Using SequencePosition:

SequencePosition[#, {1}] & /@ zeroCrossings /. {a_, a_} :> a

Using MapIndexed:

MapIndexed[If[#1 == 1, First@#2, Nothing] &, #] & /@ zeroCrossings

{{3, 9}, {4, 5, 8, 12, 13}, {4, 12, 13}, {4, 8, 11, 13}, {4, 9, 12,
13}, {5, 6, 11}}

$\endgroup$
4
$\begingroup$
SparseArray[ZeroCrossings] @ "AdjacencyLists"
{{3, 9}, {4, 5, 8, 12, 13}, {4, 12, 13}, {4, 8, 11, 13}, 
 {4, 9, 12,  13}, {5, 6, 11}}
$\endgroup$
1
  • 2
    $\begingroup$ Now the matrix tag is finally justified after all these years. $\endgroup$
    – Syed
    May 9, 2023 at 12:51
3
$\begingroup$
Catenate @* Position[1] /@ ZeroCrossings

{{3, 9}, {4, 5, 8, 12, 13}, {4, 12, 13}, {4, 8, 11, 13}, {4, 9, 12, 13}, {5, 6, 11}}

$\endgroup$
2
$\begingroup$

Another possibility is to use Position at level -1 and SplitBy as follows:

Map[#[[All, 2]] &, SplitBy[Position[ZeroCrossings, 1, -1], First]]

(*{{3, 9}, {4, 5, 8, 12, 13}, {4, 12, 13}, {4, 8, 11, 13},
   {4, 9, 12, 13}, {5, 6, 11}}*)

Or using Count and PartitionRagged with Position at level -1:

#[[All, 2]] & /@ Internal`PartitionRagged[Position[#, 1, -1], 
Count[#, 1] & /@ #] &@ZeroCrossings

(*{{3, 9}, {4, 5, 8, 12, 13}, {4, 12, 13}, {4, 8, 11, 13},
   {4, 9, 12, 13}, {5, 6, 11}}*)
$\endgroup$
2
$\begingroup$

Since @kglr suggested, already, the use of SparseArray here's another way to go about it

Flatten /@ 
 GatherBy[SparseArray[#]["NonzeroPositions"] & /@ ZeroCrossings, 
  First]

{{3, 9}, {4, 5, 8, 12, 13, 4, 12, 13, 4, 8, 11, 13, 4, 9, 12, 13}, {5, 6, 11}}

$\endgroup$
1
  • 1
    $\begingroup$ (+1) Nicely done, mate! :-) $\endgroup$ Dec 20, 2023 at 3:27
1
$\begingroup$

Using SubsetPosition (new in 12.1)

Catenate @ SubsetPosition[#, {1}] & /@ ZeroCrossings

{{3, 9}, {4, 5, 8, 12, 13}, {4, 12, 13}, {4, 8, 11, 13}, {4, 9, 12, 13}, {5, 6, 11}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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