Is there an inverse function to column_stack? i.e. I want to iterate over the columns of a matrix. I could first transpose it and then call tolist, but I was wondering if there was some clean, fast, built in way that I wasn't aware of. Thanks, Ryan
Ryan Krauss wrote:
Is there an inverse function to column_stack? i.e. I want to iterate over the columns of a matrix. I could first transpose it and then call tolist, but I was wondering if there was some clean, fast, built in way that I wasn't aware of.
Thanks,
Ryan
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.net http://www.scipy.net/mailman/listinfo/scipy-user
a = rand(4*3) a array([ 0.45210275, 0.45554869, 0.27096599, 0.72289623, 0.28874549, 0.60064951, 0.36406786, 0.97709256, 0.68812732, 0.73081783, 0.07033917, 0.50299293]) a = transpose(reshape(a,(4,3))) a array([[ 0.45210275, 0.72289623, 0.36406786, 0.73081783], [ 0.45554869, 0.28874549, 0.97709256, 0.07033917], [ 0.27096599, 0.60064951, 0.68812732, 0.50299293]])
Nils
I think we misunderstood one another. I want to iterate over the columns of a matrix I already have. This does what I want, I just wondered if there was one built-in function that does this (or if tolist could have an axis argument): In [54]: a=rand(4,3) In [55]: a Out[55]: array([[ 0.98852747, 0.63751158, 0.49660263], [ 0.46143525, 0.19731989, 0.9668494 ], [ 0.78013891, 0.5187305 , 0.74449523], [ 0.67888293, 0.80072502, 0.10930396]]) In [56]: a[0] Out[56]: array([ 0.98852747, 0.63751158, 0.49660263]) In [57]: b=(a.tra a.trace a.transpose In [57]: b=(a.transpose()).tolist() In [58]: b Out[58]: [[0.98852746688978599, 0.4614352515957133, 0.7801389135899337, 0.67888292673582395], [0.63751157798107616, 0.19731989336625744, 0.5187305017688113, 0.80072502069726714], [0.49660262671527189, 0.96684939764919231, 0.74449523248153027, 0.10930396251760111]] In [59]: b[0] Out[59]: [0.98852746688978599, 0.4614352515957133, 0.7801389135899337, 0.67888292673582395] Ryan On 2/21/06, Nils Wagner <nwagner@mecha.uni-stuttgart.de> wrote:
Ryan Krauss wrote:
Is there an inverse function to column_stack? i.e. I want to iterate over the columns of a matrix. I could first transpose it and then call tolist, but I was wondering if there was some clean, fast, built in way that I wasn't aware of.
Thanks,
Ryan
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.net http://www.scipy.net/mailman/listinfo/scipy-user
a = rand(4*3) a array([ 0.45210275, 0.45554869, 0.27096599, 0.72289623, 0.28874549, 0.60064951, 0.36406786, 0.97709256, 0.68812732, 0.73081783, 0.07033917, 0.50299293]) a = transpose(reshape(a,(4,3))) a array([[ 0.45210275, 0.72289623, 0.36406786, 0.73081783], [ 0.45554869, 0.28874549, 0.97709256, 0.07033917], [ 0.27096599, 0.60064951, 0.68812732, 0.50299293]])
Nils
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.net http://www.scipy.net/mailman/listinfo/scipy-user
You can always do def rows(A): nr_rows = x.shape[0] for r in range(nr_rows): yield A[r,:] for r in rows(A): print r Cheers Stéfan On Tue, Feb 21, 2006 at 10:11:18AM -0500, Ryan Krauss wrote:
I think we misunderstood one another. I want to iterate over the columns of a matrix I already have. This does what I want, I just wondered if there was one built-in function that does this (or if tolist could have an axis argument):
A.tolist() will already iterate over the rows of a matrix, but your suggestion could easily be adapted to a cols function. On 2/21/06, Stefan van der Walt <stefan@sun.ac.za> wrote:
You can always do
def rows(A): nr_rows = x.shape[0] for r in range(nr_rows): yield A[r,:]
for r in rows(A): print r
Cheers Stéfan
On Tue, Feb 21, 2006 at 10:11:18AM -0500, Ryan Krauss wrote:
I think we misunderstood one another. I want to iterate over the columns of a matrix I already have. This does what I want, I just wondered if there was one built-in function that does this (or if tolist could have an axis argument):
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.net http://www.scipy.net/mailman/listinfo/scipy-user
On Tue, Feb 21, 2006 at 11:39:03AM -0500, Ryan Krauss wrote:
A.tolist() will already iterate over the rows of a matrix, but your suggestion could easily be adapted to a cols function.
Also, the generator below does not copy data, unlike tolist.
On 2/21/06, Stefan van der Walt <stefan@sun.ac.za> wrote:
You can always do
def rows(A): nr_rows = x.shape[0] for r in range(nr_rows): yield A[r,:]
for r in rows(A): print r
That's a good point. Thanks Stefan. I guess my main question is, is there a built-in way to do this already. If not, a cols function like your rows is what I will likely do. (For now I am actually doing (A.transpose()).tolist()). Thanks, Ryan On 2/21/06, Stefan van der Walt <stefan@sun.ac.za> wrote:
On Tue, Feb 21, 2006 at 11:39:03AM -0500, Ryan Krauss wrote:
A.tolist() will already iterate over the rows of a matrix, but your suggestion could easily be adapted to a cols function.
Also, the generator below does not copy data, unlike tolist.
On 2/21/06, Stefan van der Walt <stefan@sun.ac.za> wrote:
You can always do
def rows(A): nr_rows = x.shape[0] for r in range(nr_rows): yield A[r,:]
for r in rows(A): print r
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.net http://www.scipy.net/mailman/listinfo/scipy-user
Ryan Krauss wrote:
Is there an inverse function to column_stack? i.e. I want to iterate over the columns of a matrix. I could first transpose it and then call tolist, but I was wondering if there was some clean, fast, built in way that I wasn't aware of.
You can iterate over arrays. In [1]: a = reshape(arange(12),(3,4)) In [2]: a Out[2]: array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) In [3]: for x in a: ...: print x ...: ...: [0 1 2 3] [4 5 6 7] [ 8 9 10 11] In [4]: for x in transpose(a): ...: print x ...: ...: [0 4 8] [1 5 9] [ 2 6 10] [ 3 7 11] -- Robert Kern robert.kern@gmail.com "In the fields of hell where the grass grows high Are the graves of dreams allowed to die." -- Richard Harter
participants (4)
-
Nils Wagner -
Robert Kern -
Ryan Krauss -
Stefan van der Walt