[Tutor] a code question, but don't know question's name
Eike Welk
eike.welk at gmx.net
Sun Oct 7 23:20:52 CEST 2007
On Sunday 07 October 2007 21:29, Happy Deer wrote:
> Thank all for the discussion.
> Maybe I can separate my question into two.
>
> First, I have experience in Matlab, where I can use "eval". I
> wonder whether someone knows about it.
I suspect you are using Numpy, you should subscribe to the Numpy
mailing-list. There are people that can compare Numpy to Matlab.
Probably you should also subscribe to the Scipy mailing list(s).
http://www.scipy.org/Mailing_Lists
>
> Second, if I just want to return data[:,1], ...data[:,-1]
> separately without knowing ahead how many columns data has. What
> should I do?
eike at bookxie:~> ipython --pylab
<much text deleted>
In [1]:a = array([[1,2,3], [4,5,6]])
In [2]:a
Out[2]:
array([[1, 2, 3],
[4, 5, 6]])
In [3]:a[:,0]
Out[3]:array([1, 4])
In [4]:a.shape
Out[4]:(2, 3)
In [6]:for i in range(a.shape[1]):
.6.: print a[:,i]
.6.:
[1 4]
[2 5]
[3 6]
If you want a[:,i] be/stay a column vector use the matrix class. The
mat constructor function even has Matlab like syntax.
In [8]:import numpy as np
In [9]:a = np.mat("1,2,3; 4,5,6")
In [11]:a
Out[11]:
matrix([[1, 2, 3],
[4, 5, 6]])
In [12]:a[:,0]
Out[12]:
matrix([[1],
[4]])
I shortened the Ipython session a little, you don't want to see my
syntax errors I guess.
HTH,
Eike.
More information about the Tutor
mailing list