作为一个 Matlab 用户切换到 python 时,我感到很沮丧,因为我不知道所有的技巧,并且在代码工作之前被困在一起。下面是一个示例,其中我有一个要添加虚拟列的矩阵。当然,还有比下面的 zip vstack zip 方法更简单的方法。它有效,但这完全是一个菜鸟尝试。请赐教。提前感谢您抽出宝贵时间阅读本教程。
# BEGIN CODE
from pylab import *
# Find that unlike most things in python i must build a dummy matrix to
# add stuff in a for loop.
H = ones((4,10-1))
print "shape(H):"
print shape(H)
print H
### enter for loop to populate dummy matrix with interesting data...
# stuff happens in the for loop, which is awesome and off topic.
### exit for loop
# more awesome stuff happens...
# Now I need a new column on H
H = zip(*vstack((zip(*H),ones(4)))) # THIS SEEMS LIKE THE DUMB WAY TO DO THIS...
print "shape(H):"
print shape(H)
print H
# in conclusion. I found a hack job solution to adding a column
# to a numpy matrix, but I'm not happy with it.
# Could someone educate me on the better way to do this?
# END CODE