2

I have:

x = np.zeros((96,11,11,2,10),dtype=np.float64)
y = np.array([0,10,20,30,40,50,60,70,80,90,100],dtype=np.float64)
x[:,:,:,0,0] = y
print x[0,:,:,0,0]

i get:

[[   0.   10.   20.   30.   40.   50.   60.   70.   80.   90.  100.]
 [   0.   10.   20.   30.   40.   50.   60.   70.   80.   90.  100.]
 [   0.   10.   20.   30.   40.   50.   60.   70.   80.   90.  100.]
 [   0.   10.   20.   30.   40.   50.   60.   70.   80.   90.  100.]
 [   0.   10.   20.   30.   40.   50.   60.   70.   80.   90.  100.]
 [   0.   10.   20.   30.   40.   50.   60.   70.   80.   90.  100.]
 [   0.   10.   20.   30.   40.   50.   60.   70.   80.   90.  100.]
 [   0.   10.   20.   30.   40.   50.   60.   70.   80.   90.  100.]
 [   0.   10.   20.   30.   40.   50.   60.   70.   80.   90.  100.]
 [   0.   10.   20.   30.   40.   50.   60.   70.   80.   90.  100.]
 [   0.   10.   20.   30.   40.   50.   60.   70.   80.   90.  100.]]

but I want the output (for any i in first dimension) to be the transpose of this. ie fill columns instead of rows

Any suggestions?

5
  • Is this your actual example? Because I don't see how you can assign a 1D array to a 3D slice, and when I try it, as expected, x[:,:,:,0,0] = y raises ValueError: operands could not be broadcast together with shapes (20,11,11) (10). Commented Sep 12, 2013 at 9:06
  • no, its not. I'll edit it now Commented Sep 12, 2013 at 9:07
  • now its the real example Commented Sep 12, 2013 at 9:15
  • I asked the question again in a better way here: stackoverflow.com/questions/18763717/… Commented Sep 12, 2013 at 12:42
  • 1
    @Sammy It is recommend that you edit your question then to ask a new one if the question is, in effect, the same. Commented Sep 12, 2013 at 12:54

3 Answers 3

3

You need to change y from 1D to 2D (with one column):

x[:,:,:,0,0] = y[:, np.newaxis]

or,

x[:,:,:,0,0] = y.reshape(11,1)
Sign up to request clarification or add additional context in comments.

Comments

2

If you want the output to be the transpose, just do:

>>> import numpy as np
>>> x = np.zeros((96,11,11,2,10),dtype=np.float64)
>>> y = np.array([0,10,20,30,40,50,60,70,80,90,100],dtype=np.float64)
>>> for i in range(x.shape[0]):
>>>    x[i,:,:,0,0] = x[i,:,:,0,0].T
>>> print x[0,:,:,0,0]
 [[   0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.]
  [  10.   10.   10.   10.   10.   10.   10.   10.   10.   10.   10.]
  [  20.   20.   20.   20.   20.   20.   20.   20.   20.   20.   20.]
  [  30.   30.   30.   30.   30.   30.   30.   30.   30.   30.   30.]
  [  40.   40.   40.   40.   40.   40.   40.   40.   40.   40.   40.]
  [  50.   50.   50.   50.   50.   50.   50.   50.   50.   50.   50.]
  [  60.   60.   60.   60.   60.   60.   60.   60.   60.   60.   60.]
  [  70.   70.   70.   70.   70.   70.   70.   70.   70.   70.   70.]
  [  80.   80.   80.   80.   80.   80.   80.   80.   80.   80.   80.]
  [  90.   90.   90.   90.   90.   90.   90.   90.   90.   90.   90.]
  [ 100.  100.  100.  100.  100.  100.  100.  100.  100.  100.  100.]]

It updates the first dimension, this is the output for 34th index:

>>> print x[34,:,:,0,0]
 [[   0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.]
  [  10.   10.   10.   10.   10.   10.   10.   10.   10.   10.   10.]
  [  20.   20.   20.   20.   20.   20.   20.   20.   20.   20.   20.]
  [  30.   30.   30.   30.   30.   30.   30.   30.   30.   30.   30.]
  [  40.   40.   40.   40.   40.   40.   40.   40.   40.   40.   40.]
  [  50.   50.   50.   50.   50.   50.   50.   50.   50.   50.   50.]
  [  60.   60.   60.   60.   60.   60.   60.   60.   60.   60.   60.]
  [  70.   70.   70.   70.   70.   70.   70.   70.   70.   70.   70.]
  [  80.   80.   80.   80.   80.   80.   80.   80.   80.   80.   80.]
  [  90.   90.   90.   90.   90.   90.   90.   90.   90.   90.   90.]
  [ 100.  100.  100.  100.  100.  100.  100.  100.  100.  100.  100.]]

4 Comments

Sorry for my confusing wording. I actually want the output to look like the transpose of this (don't want the actual transpose)
ok, take a look now, you only need to reassign x[0,:,:,0,0].T to x[0,:,:,0,0]
My point is I want x[1,:,:,0,0] , x[2,:,:,0,0], x[3,:,:,0,0] .....x[95,:,:,0,0] to look like this too. so Im looking for something in the way I initially write to x
You don't mention that on your question, you didn't say anything about all dimension but only the first one. In any case, I have updated the answer so now all x[i,:,:,0,0] dimensions are updated
0

The problem is simple: you're using a row vector for y instead of a column vector, so it's filling by row instead of by column.

More technically, you've got an array of shape (11,), instead of an array of (11, 1), so it broadcasts to (1, 11) when filling a 2D array.

Compare:

>>> x = np.zeros((96,11,11,2,10),dtype=np.float64)
>>> y = np.array([[0],[10],[20],[30],[40],[50],[60],[70],[80],[90],[100]],dtype=np.float64)
>>> x[:,:,:,0,0]=y
>>> print x[0,:,:,0,0]
[[   0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.]
 [  10.   10.   10.   10.   10.   10.   10.   10.   10.   10.   10.]
 [  20.   20.   20.   20.   20.   20.   20.   20.   20.   20.   20.]
 [  30.   30.   30.   30.   30.   30.   30.   30.   30.   30.   30.]
 [  40.   40.   40.   40.   40.   40.   40.   40.   40.   40.   40.]
 [  50.   50.   50.   50.   50.   50.   50.   50.   50.   50.   50.]
 [  60.   60.   60.   60.   60.   60.   60.   60.   60.   60.   60.]
 [  70.   70.   70.   70.   70.   70.   70.   70.   70.   70.   70.]
 [  80.   80.   80.   80.   80.   80.   80.   80.   80.   80.   80.]
 [  90.   90.   90.   90.   90.   90.   90.   90.   90.   90.   90.]
 [ 100.  100.  100.  100.  100.  100.  100.  100.  100.  100.  100.]]

Of course in your real code, y probably isn't a literal, but a result of some earlier computation. (And even if it is a literal, you don't want to type all those extra brackets.) So, assume y is inherently a row vector, as we have to deal with it.

So, just reshape it on the fly:

>>> x = np.zeros((96,11,11,2,10),dtype=np.float64)
>>> y = np.array([0,10,20,30,40,50,60,70,80,90,100],dtype=np.float64)
>>> x[:,:,:,0,0] = y.reshape((11, 1))

Same result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.