# Finding position of non empty matrix entries

I have a series of circles in space and I am trying to find the intersections of a given line and the circles and storing that in an array using Table. As the line will not intersect all circles the array has a number of empty entries

eg

int = {{}, {1, 2}, {}, {3, 4}, {}, {}, {}}


I am aware of Position and Cases and have had some success with something like:

Cases[int, Except[{}]]


But I cant figure out how to combine that with Position to find the position of the non empty entries in the int matrix.

Any help would be greatly appreciated.

Thanks

Is this all you need?:

Position[int, {__}, 1]

{{2}, {4}}

• Yes this is exactly the thing, excellent, you are in fact a wizard. Thanks. I can't vote you up unfortunately because "Vote Up requires 15 reputation" Mar 13, 2015 at 22:20
• @Whose I am glad I could help. Nevertheless this is probably too simple a question I will likely be closed. Please understand that I don't mean that to be insulting; Mathematica takes some getting used to and the pattern behavior can be subtle and complicated. Mar 13, 2015 at 22:34
• Just for the record, the OP's pattern Except@{} is actually a shade faster than {__}. Mar 13, 2015 at 23:54
• @MartinBüttner and Transpose@{Pick[Range@Length@int, Unitize[Length /@ int], 1]} will be faster yet...
– ciao
Mar 14, 2015 at 1:08
• @rasher I would use SparseArray[Length /@ int]["NonzeroPositions"] myself for that goal. ;-) Mar 14, 2015 at 7:23

Using PositionIndex:

list = {{}, {1, 2}, {}, {3, 4}, {}, {}, {}};

PositionIndex[list] // KeyDrop[{{}}] // Values


{{2}, {4}}

With

int = {{}, {1, 2}, {}, {3, 4}, {}, {}, {}}


we do

Values[KeyDrop[PositionIndex[int], Key[{}]]]


to get

{{2}, {4}}

WolframLanguageData["KeyDrop", {"VersionIntroduced",
"DateIntroduced"}]


Using SubsetPosition:

int = {{}, {1, 2}, {}, {3, 4}, {}, {}, {}};

SubsetPosition[int, {{__}}]

(*{{2}, {4}}*)


Or using ReplaceList:

ReplaceList[int, {__, x : {__}, __} :> {Last@x}]

(*{{2}, {4}}*)

list = {{}, {1, 2}, {}, {3, 4}, {}, {}, {}};


Using SequencePosition (new in 10.1)

p = SequencePosition[list, {Except @ {}}][[;; , 1]]


{2, 4}

list[[p]]


{{1, 2}, {3, 4}}

list = {{}, {1, 2}, {}, {3, 4}, {}, {}, {}};


Using Position and DeleteCases

Position[list, Alternatives @@ DeleteCases[list, {}]]


{{2}, {4}}