4
$\begingroup$

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

$\endgroup$
0
8
$\begingroup$

Is this all you need?:

Position[int, {__}, 1]
{{2}, {4}}
$\endgroup$
7
  • $\begingroup$ 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" $\endgroup$ – Whose Mar 13 '15 at 22:20
  • $\begingroup$ @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. $\endgroup$ – Mr.Wizard Mar 13 '15 at 22:34
  • $\begingroup$ Just for the record, the OP's pattern Except@{} is actually a shade faster than {__}. $\endgroup$ – Martin Ender Mar 13 '15 at 23:54
  • $\begingroup$ @MartinBüttner and Transpose@{Pick[Range@Length@int, Unitize[Length /@ int], 1]} will be faster yet... $\endgroup$ – ciao Mar 14 '15 at 1:08
  • 1
    $\begingroup$ @rasher I would use SparseArray[Length /@ int]["NonzeroPositions"] myself for that goal. ;-) $\endgroup$ – Mr.Wizard Mar 14 '15 at 7:23

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.