2
$\begingroup$

I want to select elements with the true value (Indices list) in the corresponding point to the value list (MCS). Is there anything like Matlab-style Find to get the indices of true values in the boolean list and then use it like MCS[[indicesNumbers]]?

enter image description here

$\endgroup$
1
  • 7
    $\begingroup$ Check for Pick on the docs $\endgroup$
    – Rojo
    Commented Nov 9, 2013 at 19:51

3 Answers 3

5
$\begingroup$

As Rojo suggested, Pick works -- yet there may be other solutions.

enter image description here

$\endgroup$
1
  • $\begingroup$ +1 I doubt any solution will be better than this built-in, since it is quite straightforward. The third argument defaults to True so you ca do without it $\endgroup$
    – Rojo
    Commented Nov 9, 2013 at 19:57
2
$\begingroup$

Another approach is to use Position. Here's the data vector and logical vector and a way to find the indices where True occurs:

q = {True, False, False, True, False, True};
data = Range[6] + 5;
ind=Position[Boole[q], 1]
{{1},{4},{6}}

The information can then be extracted from the data vector using

Extract[data, ind]
{6, 9, 11}

Or you can bypass using Extract by direct indexing:

data[[Flatten[ind]]]
$\endgroup$
0
$\begingroup$

I think

Pick[MCS, Indices]

is the best answer (as discussed).

Just for variety:

Extract[MCS, Position[Indices, True]]

Cases[Thread[{MCS, Indices}], {x_, True} :> x]

and not particularly elegant:

Reap[Sow @@@ Thread[{MCS, Indices}], True, #2 &][[2, 1]]
$\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.