Suppose we have the matrix A:
A = [1,2,3
4,5,6
7,8,9]
I want to know if there is a way to obtain:
B = [1,2,3
4,5,6
7,8,9
7,8,9]
As well as:
B = [1,2,3,3
4,5,6,6
7,8,9,9]
This is because the function I want to implement is the following:
U(i,j) = min(A(i+1,j)^2, A(i,j)^2)
V(i,j) = min(A(i,j+1)^2, A(i,j)^2)
And the numpy.minimum seems to need two arrays with equal shapes.
My idea is the following:
np.minimum(np.square(A[1:]), np.square(A[:]))
but it will fail.
For your particular example you could use numpy.hstack and numpy.vstack:
In [11]: np.vstack((A, A[-1]))
Out[11]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[7, 8, 9]])
In [12]: np.hstack((A, A[:, [-1]]))
Out[12]:
array([[1, 2, 3, 3],
[4, 5, 6, 6],
[7, 8, 9, 9]])
An alternative to the last one is np.hstack((A, np.atleast_2d(A[:,-1]).T)) or np.vstack((A.T, A.T[-1])).T): you can't hstack a (3,) array to a (3,3) one without putting the elements in the rows of a (3,1) array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With