python - why can't x[:,0] = x[0] for a single row vector? -
i'm relatively new python i'm trying understand seems basic.
create vector:
x = np.linspace(0,2,3) out[38]: array([ 0., 1., 2.])
now why isn't x[:,0] value argument?
indexerror: invalid index
it must x[0]. have function calling calculates:
np.sqrt(x[:,0]**2 + x[:,1]**2 + x[:,2]**2)
why can't have true regardless of input? many other languages, independent of there being other rows in array. perhaps misunderstand fundamental - sorry if so. i'd avoid putting:
if len(x) == 1: norm = np.sqrt(x[0]**2 + x[1]**2 + x[2]**2) else: norm = np.sqrt(x[:,0]**2 + x[:,1]**2 + x[:,2]**2)
everywhere. surely there way around this... thanks.
edit: example of working in language matlab:
>> b = [1,2,3] b = 1 2 3 >> b(:,1) ans = 1 >> b(1) ans = 1
perhaps looking this:
np.sqrt(x[...,0]**2 + x[...,1]**2 + x[...,2]**2)
there can number of dimensions in place of ellipsis ...
see what python ellipsis object do?, , the docs of numpy basic slicing
Comments
Post a Comment