21776

Vectorized approach for masking individual slices per column

Question:

I have a numpy array:

>>> a = np.arange(20).reshape(5, -1) array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]])

I have an array of regions going in order of columns, that I would like to create a boolean mask for:

idx = np.array([[0,2], [1,3], [2,4], [1,4]])

My desired mask for this set of indices is:

array([[ True, False, False, False], [ True, True, False, True], [False, True, True, True], [False, False, True, True], [False, False, False, False]])

So column 0 has 0:2 masked, column 1 has 1:3 masked, etc. My current approach works, but I am looking for something vectorized:

def foo(a, idx): out = np.zeros(a, dtype=np.bool8) for (i, j), k in zip(idx, np.arange(a[1])): out[i:j, k] = True return out

In action:

foo(a.shape, idx) array([[ True, False, False, False], [ True, True, False, True], [False, True, True, True], [False, False, True, True], [False, False, False, False]])

Answer1:

Using <a href="https://docs.scipy.org/doc/numpy-1.13.0/user/basics.broadcasting.html" rel="nofollow">broadcasting</a> -

In [434]: r = np.arange(a.shape[0])[:,None] In [435]: (idx[:,0] <= r) & (idx[:,1] > r) Out[435]: array([[ True, False, False, False], [ True, True, False, True], [False, True, True, True], [False, False, True, True], [False, False, False, False]])

Recommend

  • Android RecyclerView Blank Space
  • MATLAB quick find of a vector in matrix
  • Create ranking for vector of double
  • Replace any string in columns with 1
  • Difficulties implementing the Hysteresis step of Canny Algorithm in Halide without define_extern fun
  • Sql indexes vs full table scan
  • C++ String tokenisation from 3D .obj files
  • C++ Armadillo Access Triangular Matrix Elements
  • Why do GeoJSON features appear like a negative photo of the features themselves?
  • possible limitation of implode function in PHP
  • Thread synchronization with syncwarp
  • NUnit 3.0 TestCase const custom object arguments
  • ASP.NET MVC 2 Preview 2 - display directory list rather than home/index
  • Android app gives error “BatteryStatsImpl: reading network stats”
  • How can I extract results of aggregate queries in slick?
  • OOP Javascript - Is “get property” method necessary?
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • PHP - How to update data to MySQL when click a radio button
  • Join two tables and save into third-sql
  • Comma separated Values
  • Error creating VM instance in Google Compute Engine
  • How to set the response of a form post action to a iframe source?
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]
  • Cant find why the layout is getting smaller
  • Change div Background jquery
  • How to stop GridView from loading again when I press back button?
  • Qt: Run a script BEFORE make
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • reshape alternating columns in less time and using less memory
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?