简体   繁体   English

无法为矩阵中的条目分配值

[英]Unable to assign values to entries in matrices

I'm trying to create a complex matrix and assign the 0,0 entry to the value of 1+0j. 我正在尝试创建一个复杂的矩阵并将0,0项分配给值1 + 0j。 Here's the code I wrote: 这是我写的代码:

import numpy as n
import cmath

M=n.zeros((5,5),dtype=complex)
M[0:0]=1+0j
print M

However, after executing, the 0,0 entry is still zero: 但是,执行后,0,0条目仍为零:

[[ 0.+0.j  0.+0.j  0.+0.j  0.+0.j  0.+0.j]
 [ 0.+0.j  0.+0.j  0.+0.j  0.+0.j  0.+0.j]
 [ 0.+0.j  0.+0.j  0.+0.j  0.+0.j  0.+0.j]
 [ 0.+0.j  0.+0.j  0.+0.j  0.+0.j  0.+0.j]
 [ 0.+0.j  0.+0.j  0.+0.j  0.+0.j  0.+0.j]]

What's wrong with my code? 我的代码有什么问题? What's the proper way to do it? 正确的做法是什么?

You just need to code your matrix element reference like this: 您只需要像这样编码矩阵元素引用即可:

    M[0,0]=1+0j

The syntax M[0:0] would mean the first sublist to just before the first sublist, ie an empty element. 语法M [0:0]表示第一个子列表在第一个子列表之前,即一个空元素。 Try evaluating M[0:1] 尝试评估M [0:1]

    >>>> M[0:1]
    array([[ 1.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM