3

Let's say I have this numpy array :

first_array = [[1. 0. 0. 0. 0.]
               [1. 0. 0. 0. 0.]
               [0. 1. 0. 0. 0.]
               [0. 0. 1. 0. 0.]
               [0. 0. 1. 0. 0.]
               [0. 0. 1. 0. 0.]
               [0. 0. 0. 0. 1.]]

and I have this 2 lists :

index_start = [50, 80, 110, 120, 150, 180, 200]
index_end= [70, 90, 115, 140, 170, 190, 220]

I want to create a new 2D numpy output_array where I iterate by column first_array and duplicate each value of every row from an index_start to an index_end.

1st iteration - For example first_array[[1,1]] = 1, index_start[1] = 50 and index_end[1]=70 then the first column of my output_array will have the value 1 from index 50 to 70.

2nd iteration - Then first_array[[2,1]] = 1, index_start[2] = 80 and index_end[2]=90 then the first column of my output_array will have also the value 1 but from index 80 to 90.

3rd iteration - first_array[[2,3]] = 1, index_start[3] = 110 and index_end[3]=115 then the second column of my output_array will have the value 1 from index 110 to 115 and so on.

Here's what I have tried but this is giving me wrong result :

first_array = [[1, 0, 0, 0, 0,],
               [1, 0, 0, 0, 0,],
               [0, 1, 0, 0, 0,],
               [0, 0, 1, 0, 0,],
               [0, 0, 1, 0, 0,],
               [0, 0, 1, 0, 0,],
               [0, 0, 0, 0, 1,]]
index_start = [50, 80, 110, 120, 150, 180, 200]
index_end= [70, 90, 115, 140, 170, 190, 220]
last_index = max(index_start+index_end)
output_array = np.zeros((last_index, 5))

for i in range(len(index_start)):
    for j in range(last_index):
        for k in range(5):
            output_array[index_start[i]:index_end[i]]=first_array[i][k]

1 Answer 1

2

It's working now. I forgot to add the k column index at the output_array. Here's the final working code if anyone needed this.

first_array = [[1, 0, 0, 0, 0,],
               [1, 0, 0, 0, 0,],
               [0, 1, 0, 0, 0,],
               [0, 0, 1, 0, 0,],
               [0, 0, 1, 0, 0,],
               [0, 0, 1, 0, 0,],
               [0, 0, 0, 0, 1,]]
index_start = [50, 80, 110, 120, 150, 180, 200]
index_end= [70, 90, 115, 140, 170, 190, 220]
last_index = max(index_start+index_end)
output_array = np.zeros((last_index+1, 5))

for i in range(len(index_start)):
    for k in range(5):
        output_array[index_start[i]:index_end[i]+1, k]=first_array[i][k]
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.