How to automatically append x,y values to a matrix

Joined
Jan 30, 2023
Messages
6
Reaction score
0
Hi, I want to add new rows to a matrix by a while loop using randomly generated points, but I am getting an error code regarding number of dimensions.
Here is an example of the code:

import random

import numpy as np

first_list = []

a = 0
while a <= 10:
first_list.append(a)
a += 1
random.shuffle(first_list)

second_list = []

b= 0
while b <= 10:
second_list.append(b)
b += 1
random.shuffle(second_list)

n = 0
xy_array = np.array([[first_list.index(n),second_list.index(n)]])

row_to_be_added =([[first_list.index(n+1),second_list.index(n+1)]])

while n<=len(first_list):
np.r_[xy_array,[row_to_be_added]]
n+=1

I get an error message that the dimensions are wrong. More specifically, I get the message "array at index 0 has 2 dimensions, but array at index 1 has 3 dimensions." I am trying to add additional rows to the matrix. What am I doing wrong?
 
Last edited:
Joined
Jan 30, 2023
Messages
107
Reaction score
13
The issue is that the row_to_be_added is a nested list with 2 dimensions, but xy_array has only 1 dimension. To resolve this, you should use np.concatenate instead of np.r_ and remove the nested list when creating row_to_be_added:

Code:
import random
import numpy as np

first_list = []
a = 0
while a <= 10:
  first_list.append(a)
  a += 1
random.shuffle(first_list)

second_list = []
b = 0
while b <= 10:
  second_list.append(b)
  b += 1
random.shuffle(second_list)

n = 0
xy_array = np.array([first_list[n], second_list[n]])

row_to_be_added = [first_list[n+1], second_list[n+1]]

while n < len(first_list)-1:
  xy_array = np.concatenate((xy_array, [row_to_be_added]), axis=0)
  n += 1

Note that np.concatenate takes the axis parameter to specify the axis along which to concatenate the arrays. In this case, we are concatenating along the first axis (0), so the new arrays are added as new rows.
 
Joined
Jan 30, 2023
Messages
6
Reaction score
0
The issue is that the row_to_be_added is a nested list with 2 dimensions, but xy_array has only 1 dimension. To resolve this, you should use np.concatenate instead of np.r_ and remove the nested list when creating row_to_be_added:

Code:
import random
import numpy as np

first_list = []
a = 0
while a <= 10:
  first_list.append(a)
  a += 1
random.shuffle(first_list)

second_list = []
b = 0
while b <= 10:
  second_list.append(b)
  b += 1
random.shuffle(second_list)

n = 0
xy_array = np.array([first_list[n], second_list[n]])

row_to_be_added = [first_list[n+1], second_list[n+1]]

while n < len(first_list)-1:
  xy_array = np.concatenate((xy_array, [row_to_be_added]), axis=0)
  n += 1

Note that np.concatenate takes the axis parameter to specify the axis along which to concatenate the arrays. In this case, we are concatenating along the first axis (0), so the new arrays are added as new rows.

Thank you very much!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top