2

How can I create a (48,64) Numpy array like this:

i,      i,      i, .....,i
i+0.1,  i+0.1,..........,i+0.1
i+0.2,  i+0.2,..........,i+0.2
.
.
.
.
i+6.3,  i+6.3,..........,i+6.3

0.1 is the fixed difference between rows.

I have solved it perfectly with JoshAdel's answer,

But how about the fixed difference is between columns?i.e.

i,i+0.1,i+0.2.....i+6.3
i,i+0.1,i+0.2.....i+6.3
.
.
.
i,i+0.1,i+0.2.....i+6.3

Thanks a lot!

2 Answers 2

2
import numpy as np
i = 10.0
a = np.empty((64,48))
a.fill(i)
a += np.arange(0,6.4,0.1)[:,np.newaxis]

Out[12]: 
array([[ 10. ,  10. ,  10. , ...,  10. ,  10. ,  10. ],
       [ 10.1,  10.1,  10.1, ...,  10.1,  10.1,  10.1],
       [ 10.2,  10.2,  10.2, ...,  10.2,  10.2,  10.2],
       ..., 
       [ 16.1,  16.1,  16.1, ...,  16.1,  16.1,  16.1],
       [ 16.2,  16.2,  16.2, ...,  16.2,  16.2,  16.2],
       [ 16.3,  16.3,  16.3, ...,  16.3,  16.3,  16.3]])

A couple of notes:

  • Numpy's shape convention is (nrow, ncolumn) so you need the shape to be (64,48) not (48,64) to the array that you have in your question.

  • There are multiple ways to do this, but I chose to use numpy's broadcasting notation.

  • You can write this more compactly, but I split it into separate steps for illustrative purposes.

Sign up to request clarification or add additional context in comments.

1 Comment

another question..how can I do it column-wise?
0

A different way to do this (just for fun) is using tile (doc)

c = 10 + np.cumsum(np.ones(64))*.1 - .1
a = np.tile(c,(48,1)).T

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.