1

So I have two arrays (row and column) row = [0, 1, 0, 2, 0, 1, 3, 1, 3, 1, 2, 4] and column = [0, 0, 1, 1, 2, 2, 2, 3, 3, 0, 1, 4]

I want to use the rows and columns arrays to insert the value "1" into another 2D array (vectors)

vectors = 
[[0. 0. 0. 0. 0.],
[0. 0. 0. 0. 0.],
[0. 0. 0. 0. 0.],
[0. 0. 0. 0. 0.],
[0. 0. 0. 0. 0.]]

so my desired output is:

vectors = 
[[1. 1. 1. 0. 0.],
[1. 0. 1. 1. 0.],
[0. 1. 0. 0. 0.],
[0. 0. 1. 1. 0.],
[0. 0. 0. 0. 1.]]

Sorry if my explanation is bad it's my first time using python and Stackoverflow.

3
  • 1
    what is the logic? vectors is 5*5 but row, column is 12*12 Commented Mar 6, 2021 at 15:56
  • 1
    What's the logic behind this? Commented Mar 6, 2021 at 15:56
  • @Epsi95 I am creating a vector from those values so I need to use the rows and columns to show the position where to add the 1's. Commented Mar 6, 2021 at 19:58

2 Answers 2

2

Just use the row and column vector as indices:

>>> vectors[row, column] = 1
>>> vectors
array([[1., 1., 1., 0., 0.],
       [1., 0., 1., 1., 0.],
       [0., 1., 0., 0., 0.],
       [0., 0., 1., 1., 0.],
       [0., 0., 0., 0., 1.]])
Sign up to request clarification or add additional context in comments.

1 Comment

Oh wow, it was that simple thanks for the help; I need to work on my python skills.
2

I think you could try this:

for r, c in zip(row, column):
    vectors[r][c] = 1

1 Comment

no problem! Actually, I learned a little something from @Cyttorak's answer, too, so that's a bonus.

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.