0

I have some code here:

u = np.zeros((4, 1))
U = np.zeros((4, 4))
# Then did some stuff to change some value in U
X = U * u
print U
print u
print X

Output:

[[ 0. -1.  2.  0.]
[ 0.  0. -1.  3.]
[ 0.  0.  0. -1.]
[ 0.  0.  0.  0.]]
[[ 0.]
 [ 0.]
 [ 0.]
 [ 0.]]
[[ 0. -0.  0.  0.]
 [ 0.  0. -0.  0.]
 [ 0.  0.  0. -0.]
 [ 0.  0.  0.  0.]]

I cannot figure out. Shouldn't X be 4 * 1 matrix? Why it is 4 * 4?

ZigZagZebra
  • 1,099
  • 2
  • 11
  • 20
  • 1
    What's the type of those variables? – Karoly Horvath Nov 11 '15 at 23:38
  • 1
    Why do you feel it should be 4 by 1? You are creating `numpy` arrays, not `numpy` matrices. Perhaps you could start by reading the documentation of the library you're using? – donkopotamus Nov 11 '15 at 23:43
  • 1
    use `np.dot` for matrix multiplication, `*` is interpreted as element wise multiplication, and `u` is then broadcasted as a 4x4 array (4 replicas of `u`) – Julien Nov 11 '15 at 23:45
  • Elaborating on what @donkopotamus, multiplication of numpy arrays is done by broadcasting. Use `dot` for matrix multiplication. (In Python 3.5+, you can use `@`.) – Alan Nov 11 '15 at 23:45
  • I will look it up. Thank you. – ZigZagZebra Nov 11 '15 at 23:49

0 Answers0