OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

add a border (filled with 0's) around an existing array

  • Thread starter Thread starter Dian He
  • Start date Start date
D

Dian He

Guest
I'm trying to add a border (filled with 0's) around an existing array. My code:

Code:
a = np.random.random((4, 5))
m, n = a.shape
b = np.zeros((m+1, n+1))
b[1:-1,1:-1] = a
a,b

but I got a error: ValueError: could not broadcast input array from shape (4,5) into shape (3,4)

Why can't I directly assign values to parts of a 2D numpy array? where is the problem?
<p>I'm trying to add a border (filled with 0's) around an existing array.
My code:</p>
<pre><code>a = np.random.random((4, 5))
m, n = a.shape
b = np.zeros((m+1, n+1))
b[1:-1,1:-1] = a
a,b
</code></pre>
<p>but I got a error:
<code>ValueError: could not broadcast input array from shape (4,5) into shape (3,4)</code></p>
<p>Why can't I directly assign values to parts of a 2D numpy array? where is the problem?</p>
 
Top