[Numpy-discussion] vec-operator

Tim Hochberg tim.hochberg at ieee.org
Thu Jul 26 12:35:27 EDT 2001


From: "Nils Wagner" <nwagner at isd.uni-stuttgart.de>
> A matrix operation is that of stacking the columns of a
> matrix one under the other to form a single column.
> This operation is called "vec" or "cs" (c)olumn (s)tring
>
> Example
>
> A is a m * n matrix
>
> vec(A) = reshape(transpose(A),(m*n,1))

I assume you mean:

def vec(A):
   reshape(transpose(A), (m*n,1))

First off, the following is a bit simpler and means you don't have to carray
m and n around

def vec(A):
   reshape(transpose(A), (-1,1))

> How can I copy the result of vec(A) into the i-th column of another
> matrix called B ?

B = zeros([m*n, p])
B[:,i:i+1] = vec(A)

However, I don't think this is what you really want. I suspect you'd be
happier with:

B[:,i] = ravel(A)

Ravel turns A into an m*n length vector [shape (m*n,)] instead of m*n by 1
array [shape (m*n,1)]. If all you want to do is insert it into B, this is
going to be more useful.

-tim







More information about the NumPy-Discussion mailing list