0

I want to define a 3d numpy array with shape = [3, 3, 5] and also with values as a range starting with 11 and step = 3 in column-wise manner. I mean:

B[:,:,0] = [[ 11,  20,  29],
            [ 14,  23,  32],
            [17,   26,  35]]

B[:,:,1] = [[ 38,  ...],
            [ 41,  ...],
            [ 44,  ...]]
...

I am new to numpy and I doubt doing it with np.arange or np.mgrid maybe. but I don't how to do.

How can this be done with minimal lines of code?

1 Answer 1

1

You can calculate the end of the range by multiplying the shape by the step and adding the start. Then it's just reshape and transpose to move the column around:

start = 11
step = 3
shape = [5, 3, 3]

end = np.prod(shape) * step + start

B = np.arange(start, end, step).reshape([5, 3, 3]).transpose(2, 1, 0)
B[:, :, 0]

# array([[11, 20, 29],
#        [14, 23, 32],
#        [17, 26, 35]])
Sign up to request clarification or add additional context in comments.

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.