Extending dimensions of a matrix
Hello All, I am trying to extend a piece of matlab code into numpy. And I am stuck at converting, ( x is a nX3 matrix) Matlab : dim = size(x,1) % would be n nx = x(:,:,ones(1,dim)) % would be nX3Xn matrix .. Now the same in Numpy seems to be tricky to me .. Can somebody help me in writing this piece of code. Thanks Santhosh
On Thu, May 26, 2011 at 21:52, santhu kumar <mesanthu@gmail.com> wrote:
Hello All,
I am trying to extend a piece of matlab code into numpy.
And I am stuck at converting, ( x is a nX3 matrix) Matlab : dim = size(x,1) % would be n nx = x(:,:,ones(1,dim)) % would be nX3Xn matrix ..
Now the same in Numpy seems to be tricky to me .. Can somebody help me in writing this piece of code.
[~] |1> n = 10 [~] |2> x = np.arange(n*3).reshape((n,3)) [~] |3> nx = x[:,:,np.newaxis][:,:,np.zeros(n, dtype=int)] [~] |4> nx.shape (10, 3, 10) [~] |5> nx array([[[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]], [[ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3], [ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4], [ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]], [[ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6], [ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8]], [[ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9], [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], [11, 11, 11, 11, 11, 11, 11, 11, 11, 11]], [[12, 12, 12, 12, 12, 12, 12, 12, 12, 12], [13, 13, 13, 13, 13, 13, 13, 13, 13, 13], [14, 14, 14, 14, 14, 14, 14, 14, 14, 14]], [[15, 15, 15, 15, 15, 15, 15, 15, 15, 15], [16, 16, 16, 16, 16, 16, 16, 16, 16, 16], [17, 17, 17, 17, 17, 17, 17, 17, 17, 17]], [[18, 18, 18, 18, 18, 18, 18, 18, 18, 18], [19, 19, 19, 19, 19, 19, 19, 19, 19, 19], [20, 20, 20, 20, 20, 20, 20, 20, 20, 20]], [[21, 21, 21, 21, 21, 21, 21, 21, 21, 21], [22, 22, 22, 22, 22, 22, 22, 22, 22, 22], [23, 23, 23, 23, 23, 23, 23, 23, 23, 23]], [[24, 24, 24, 24, 24, 24, 24, 24, 24, 24], [25, 25, 25, 25, 25, 25, 25, 25, 25, 25], [26, 26, 26, 26, 26, 26, 26, 26, 26, 26]], [[27, 27, 27, 27, 27, 27, 27, 27, 27, 27], [28, 28, 28, 28, 28, 28, 28, 28, 28, 28], [29, 29, 29, 29, 29, 29, 29, 29, 29, 29]]]) -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
participants (2)
-
Robert Kern
-
santhu kumar