0

how can I make from:

a1=2
a2=3
b1=1
b2=4

a binary vector like this:

     0,1,2,3,4
a = [0,0,1,1,0]
b = [0,1,0,0,1]

I mean that integers here define me the indexes in vectors, where it should be '1'. How is the fastest way to do this?

1
  • 1
    The fastest way would be to type [0, 0, 1, 1, 0 ] if you're typing out all those variables.... however, I'm guessing they're probably coming from a different source or in a different format? I'd suggest reading and making sure you're not falling into the XY Problem Commented Aug 28, 2017 at 10:42

3 Answers 3

2

How about simply using a1 and a2 as indices like so:

a = [0] * 5
a[a1] = 1
a[a2] = 1
Sign up to request clarification or add additional context in comments.

Comments

0

For your example, it would be

a = [0] * 5
b = [0] * 5
a1 = 2
a2 = 3
b1 = 1
b2 = 4

a[a1] = 1
a[a2] = 1
b[b1] = 1
b[b2] = 1

Comments

0

You could do something like this. This way it could work for every amount of indexes without hardcoding anything more than needed.

oneIndexesA = [a1,a2]
oneIndexesB = [b1,b2]

a = [0] * (max(oneIndexesA) + 1)
for x in oneIndexesA:  
    a[x] = 1

b = [0] * (max(oneIndexesB) + 1)
for x in oneIndexesB:  
    b[x] = 1

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.