2

I am trying to use an array (mask) which has binary values to modify another array (data), in order to keep only the values where mask is equal to 1

This is my MATLAB example code:

mask = [0 0 0 1 1 0 0];

data = [1 2 3 4 5 6 7];

data(mask == 0) = []

My result is then:

data = [4 5]

What is the equivalent operation to accomplish this in Python?

1
  • 1
    Don't understand the downvote. People switching over between Python and MATLAB ask this question all the time. Commented Nov 11, 2014 at 18:56

4 Answers 4

1

You can look at Boolean index arrays in numpy which do exactly what you describe.

For example, your code in numpy flavor looks like:

In [38]: import numpy as np
In [39]: mask = np.array([False, False, False, True, True, False, False])
In [40]: data = np.array([1, 2, 3, 4, 5, 6, 7])
In [41]: data[mask]

Out[41]: array([4, 5])

In general, if you are looking to create a MATLAB like environment in Python, you should try to use numpy, pandas, ipython and matplotlib together. These are all excellent libraries that replicate the MATLAB ecosystem and in some cases give you even more powerful tools.

Sign up to request clarification or add additional context in comments.

3 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
Sure. I will update my answer shortly. No more down votes please :)
Ok this is the best solution so far! Thanks
1

If you want this purely in Python, you could use filter to filter out those entries in your data list that contain a 1 in the corresponding entries of mask. Make a list of tuples that combine the data and mask together, use filter and use the first element of each tuple to determine which elements in this list of tuples to keep, then simply extract the data part when you're done using map. Something like:

In [1]: mask = [0,0,0,1,1,0,0]

In [2]: data = [1,2,3,4,5,6,7]

In [3]: L = [(x,y) for (x,y) in zip(mask,data)]

In [4]: F = filter(lambda z: z[0], L)

In [5]: data = map(lambda z: z[1], F)

In [5]: data
Out[5]: [4, 5]

However, if you want to best do the logical indexing efficiently, I would use numpy to do that for you. Create a numpy array of your mask and data, then use logical indexing like in MATLAB to get what you want:

In [6]: import numpy as np

In [7]: L = np.array(data)

In [8]: M = np.array(mask, dtype='bool')

In [9]: data = list(L[M])

In [10]: data
Out[10]: [4, 5]

If you're coming from MATLAB and are switching over to Python, numpy is the way to go. The syntax used to slice through the arrays and the built-in functions used on numpy arrays is very akin to how you'd do the same operations in MATLAB.

Good luck!

Comments

0

Even for a general array (meaning non-boolean), this can be achieved by directly using the mask as the index.

In [16]: a
Out[16]: array([1, 2])

In [17]: b
Out[17]: 
array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10],
       [11, 12, 13, 14, 15]])

In [18]: b[a]
Out[18]: 
array([[ 6,  7,  8,  9, 10],
       [11, 12, 13, 14, 15]])

In [19]: b[:, a]
Out[19]: 
array([[ 2,  3],
       [ 7,  8],
       [12, 13]])

Comments

-1

This is what I got. However, if you want cleaner code then you could wait for some bigwigs to answer:

mask = [0,0,0,1,1,0,0]
data = [1,2,3,4,5,6,7]

for index, one in enumerate(mask):
    if one == 1:
        print data[index],
print

Output : 4,5

2 Comments

Ok in this way I can access the right elements, but how to delete these elements from data?
You can write these values into a new list.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.