Python Forum

Full Version: numpy in1d with two simple arrays
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have this Matlab code

T = L(find(ismember(L+1,L)));

and I want to replicate it into Python.

This is my L array

L = np.array([ 1,  3,  5,  7, 12, 13, 14, 15, 16, 17, 18, 24, 25, 27, 29, 30, 31, 32, 33, 35, 36, 38, 41, 43])
I went for steps, translating first ismember(L+1,L) with

w = np.in1d(L+1, L)
Now for the rest I tried

T = L[np.where(w[w is True])]
but I get
[ ]
as result.

While correct result should be

11, 12, 13, 14, 15, 16, 23, 28, 29, 30, 31, 34
I suspect that you want to do something like this L[np.in1d(L+1, L)]. But I cann't understand why you have 11 in the result array stated as correct. Neither L, nor L+1 could include such number.
(Sep-19-2020, 12:16 AM)scidam Wrote: [ -> ]I suspect that you want to do something like this L[np.in1d(L+1, L)]. But I cann't understand why you have 11 in the result array stated as correct. Neither L, nor L+1 could include such number.

I tried that but I get
IndexError: boolean index did not match indexed array along dimension 0; dimension is 1 but corresponding boolean dimension is 24
Just tried and everything works fine:

>>> import numpy as np
>>> L = np.array([ 1,  3,  5,  7, 12, 13, 14, 15, 16, 17, 18, 24, 25, 27, 29, 30, 31, 32, 33, 35, 36, 38, 41, 43])
>>> L[np.in1d(L+1, L)]
array([12, 13, 14, 15, 16, 17, 24, 29, 30, 31, 32, 35])