I have small problem but I can’t just find an easy answer.I feel stupid for asking it.
How can i multiply a scalar with a numpy.ndarray?
import fileinput,sys,re,csv,scipy,os,numpy,pylab
from collections import defaultdict
from matplotlib.pyplot import *
from StringIO import StringIO
import numpy as num
a = open("testt.txt", "r")
b=[ raw.strip().split() for raw in a]
c=np.array(b)
d=c.transpose()
data=np.loadtxt("uu.txt",skiprows=1,dtype=None,delimiter='\t')
t1 = data[:,0]
t=(1/1000)*t1
s = data[:,9]
pylab.plot(t1, s)
pylab.xlabel('time (ms)')
pylab.ylabel('Zone height (mm)')
pylab.grid(True)
pylab.savefig('simple_plot')
pylab.show()
The error is in the line t=(1/1000)*t1 which gives me the error:
TypeError: unsupported operand type(s) for *: ‘int’ and ‘numpy.ndarray’. The text file uu.txt is an 60*60 matrix with an header as the first line.I can post it if its necessary.
Thanks
It’s a tad surprising. If
datais andarray, thent1=data[:,0]is andarraytoo and you shouldn’t have any problem multiplying it by an int.Still:
t1as well as its.shape.t1to be andarrayjust in case:t1=np.asarray(data[:,0])(1/1000)but(1./1000)instead:(1/1000)is0by virtue of integer division…