Python Forum
numpy in1d with two simple arrays
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
numpy in1d with two simple arrays
#1
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
Reply
#2
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.
Reply
#3
(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
Reply
#4
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])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem adding two numpy arrays djf123 2 2,042 Aug-09-2022, 08:31 PM
Last Post: deanhystad
  how to join by stack multiple types in numpy arrays caro 1 1,102 Jun-20-2022, 05:02 PM
Last Post: deanhystad
  numpy.dot() result different with classic computation for large-size arrays geekgeek 5 1,829 Jan-25-2022, 09:45 PM
Last Post: Gribouillis
  Two numpy arrays Sandra2312 1 1,777 Jan-18-2021, 06:10 PM
Last Post: paul18fr
  Type coercion with Numpy arrays Mark17 2 2,486 Jul-24-2020, 02:04 AM
Last Post: scidam
  filling and printing numpy arrays of str pjfarley3 4 3,205 Jun-07-2020, 09:09 PM
Last Post: pjfarley3
  How to concatenate nested numpy arrays? python_newbie09 2 4,726 Apr-16-2019, 07:00 PM
Last Post: python_newbie09

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020