Menu
Newbedev LogoNEWBEDEV Python Javascript Linux Cheat sheet
Newbedev LogoNEWBEDEV
  • Python 1
  • Javascript
  • Linux
  • Cheat sheet
  • Contact

Delete diagonals of zero elements

One way using numpy.pad:

n = my_array.shape[1] - 1
np.dstack([np.pad(a, (i, n-i), "constant") 
           for i, a in enumerate(my_array.T)])

Output:

array([[[45,  0,  0],
        [ 4, 48,  0],
        [10,  4,  1],
        [37, 20, 40],
        [25, 24, 22],
        [ 0, 25,  7],
        [ 0,  0, 13]]])

Tags:

Python

Numpy

Numpy Ndarray

Diagonal

Related

Firebase storage artifacts is huge and keeps increasing Why std::future is different returned from std::packaged_task and std::async? Accessing external storage in Android API 29 Compare current row value to previous row values What does triple colon (:::) in a data type mean in haskell? R: Group Similar Addresses Together Duplicate class com.google.android.gms.internal.vision.zze found in modules Does C# perform short circuit evaluation of if statements with await? Unable to use Laravel Factory in Tinker Exception in thread "main" java.lang.NumberFormatException: For input string: "9000000000000000" under radix 16 Sending Push Notifications to iOS from PWA 'React' was used before it was defined

Recent Posts

Pandas how to find column contains a certain value Recommended way to install multiple Python versions on Ubuntu 20.04 Build super fast web scraper with Python x100 than BeautifulSoup How to convert a SQL query result to a Pandas DataFrame in Python How to write a Pandas DataFrame to a .csv file in Python
© 2021 newbedevPrivacy Policy