2

What I have:

>>> forces
array([[[  63.82078252,    0.63841691],
        [ -62.45826693,    7.11946976],
        [ -87.85946925,   15.1988562 ],
        [-120.49417797,  -16.31785819],
        [ -81.36080338,    6.45074645]],

       [[ 364.99959095,    4.92473888],
        [ 236.5762723 ,   -7.22959548],
        [  69.55657789,    1.20164815],
        [ -22.1684177 ,   13.42611095],
        [ -91.19739147,  -16.15076634]]])

forces[0] and forces [1] each contain a list of paired values, e.g. 63.82078252 & 0.63841691 are one data point.

I want to remove all pairs where the first value is negative:

>>> forces
array([[[  63.82078252,    0.63841691]],

       [[ 364.99959095,    4.92473888],
        [ 236.5762723 ,   -7.22959548],
        [  69.55657789,    1.20164815]]])

But this type of structure is not possible since the two slices of forces have different sizes: (1, 2) and (3, 2) respectively.

My sloppy attempt:

>>> forces[:,:,0][forces[:,:,0] < 0] = np.nan
>>> forces
array([[[  63.82078252,    0.63841691],
        [          nan,    7.11946976],
        [          nan,   15.1988562 ],
        [          nan,  -16.31785819],
        [          nan,    6.45074645]],

       [[ 364.99959095,    4.92473888],
        [ 236.5762723 ,   -7.22959548],
        [  69.55657789,    1.20164815],
        [          nan,   13.42611095],
        [          nan,  -16.15076634]]])

and then using isnan to remove the relevant entries:

>>> forces = forces[~np.isnan(forces).any(axis=2)]
>>> forces
array([[  63.82078252,    0.63841691],
       [ 364.99959095,    4.92473888],
       [ 236.5762723 ,   -7.22959548],
       [  69.55657789,    1.20164815]])

So these are the correct values but they are now lumped together into a 2D array.

How can I create a heterogeneously sized "array" that will contain two slices of size (1, 2) and (3, 2) respectively, for this simplified example?

Also any pointers on accomplishing the task more elegantly would be much appreciated!

1 Answer 1

6

It is simply

forces[forces[..., 0] >= 0]

Read more here: http://scipy-lectures.github.io/intro/numpy/array_object.html#fancy-indexing

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

6 Comments

That is really good to know (and certainly much cleaner) but it still returns a 2D array.
I'm not clear on what what shape do you expect to get. forces.shape is (2, 5, 2), so each slice forces[i, ...] has shape (5, 2), i.e. contains five paired values. In your example, filtering slice forces[0, ...] leaves us with one paired set of values and filtering slice forces[1, ...] leaves us with three paired values. How do they sit together in a 3D array?
I think I see my problem. What I was envisioning was forces having 2 slices of shape (1, 2) and (3, 2) respectively which isn't possible in numpy. Any idea on how to achieve a similar result?
That depends on what your data represents. Are forces[0, 0, :] and forces[1, 0, :], i.e. array([ 63.82078252, 0.63841691]) and array([ 364.99959095, 4.92473888]) supposed to be linked in some way? What are you modelling? Why are there two sets of forces?
One slice, e.g. forces[0], contains a set of x- and y-directed force values at different intervals. forces[1] contains equivalent data for a different system. I simplified the example but I have many more systems and want to plot each system as its own line on a plot but only the points with x-values larger than zero.
|

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.