1

I have a numpy array of dimension 3x3 i.e. it has a total of 9 elements. I have 8 values that I would like to set and the value at location (2, 2) will always be 1. I can do it individually as follows:

import numpy as np
def set(coefs):
    a = np.zeros(3,3)
    a[0, 0] = coefs[0]
    a[0, 1] = coefs[1]
    a[0, 2] = coefs[2]
    a[1, 0] = coefs[3]

    ...
    a[2, 2] = 1

However, I was wondering if there is a more concise and general way to fill those 8 values.

1 Answer 1

3

Seems like

np.array(coefs[:8]+[1]).reshape((3,3))

should do exactly what you require, right?

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

4 Comments

That did the trick! Thanks. I am still getting used to these funky python addressing!
Ciao @Luca, this is more numpy 2-d addressing than Python addressing per se:-). Anyway, please make sure to accept this answer (click on the checkmark-shaped outline to the left of the A) once enough time has elapsed for acceptance (I can never recall how long you must wait before accepting depending on your rep:-).
I have to wait 10 minutes before accepting the answer ;-) You were too fast :)
@Luca, scusami, this community's so awesome that unless I answer at once I'll never get accepts!-)

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.