Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filling a vector with a varying amount of variables

Hey currently I’m trying to program a quadratic programming algorithm with Python.
But I’m struggling with creating a vector which indicates the dimensions of the problem.

My problem: I am trying to create a vector x = [x_1, x_2, x_3, …, x_N] which containts N elements. N varies.The elements ‘x_N’ should be variables / the solution of my optimization problem.

My code so far: (‘NoE’… is equal to N+1, thats why Im substracting 1 in my while statement)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 #filling the x-vector according to the dimension of the given problem
    temp_2 = 0
    while temp_2 <= (NoE-1):   
        x[temp_2]= 'x_' + temp_2
        print(x)

Thanks for helping! 🙂

>Solution :

You have to create an empty vector x and append to it 'x_' and convert temp_2 into a string in each iteration and concatenate it to 'x_', also the incrementation is missing in the code.

The following is the correct code:

temp_2 = 0

NoE=10

x=[] # Create an empty vector

while temp_2 <= (NoE-1):
   
    x.append('x_' + str(temp_2))

    temp_2 += 1 

print(x)

Output

['x_0', 'x_1', 'x_2', 'x_3', 'x_4', 'x_5', 'x_6', 'x_7', 'x_8', 'x_9']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading