Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I change a two dimensional array to one dimensional

Tags:

I am trying to change a two dimensional array to one dimensional, my code is like this:

x = np.array([[1, 2, 4], [3, 4], [1,2,3,4,5,6,7]])
x = x.flatten()

however, I found that flatten function works well on

x = np.array([[1, 2], [3, 4]])

but it does not work on

x = np.array([[1, 2, 4], [3, 4], [1,2,3,4,5,6,7]])

could anyone help me to change

np.array([[1, 2, 4], [3, 4], [1,2,3,4,5,6,7]]) 

to

np.array([[1, 2, 4, 3, 4, 1,2,3,4,5,6,7])

thank you

like image 309
chengjun zhang Avatar asked Oct 25 '18 02:10

chengjun zhang


People also ask

How do you convert a two dimensional array to a one-dimensional array?

In this C# Program, we are reading the elements of the 2-Dimensional matrix. Using for loop assign the value of 'a[i,j]' variable to b[] array variable. Increment the value of base index 'k' variable. Print the value of one dimensional array.

How do you swap a 2D array?

I write this method for swapping two elements in a 2D array: public void swap(Object[][] array, int a, int b) { Object temp; temp = array[a]; array[a] = array[b]; array[b] = temp; // Error, Why? }

How do you convert 1D into 2D?

Convert a 1D array to a 2D Numpy array using numpy. Here, we are using np. reshape to convert a 1D array to 2 D array. You can divide the number of elements in your array by ncols.


1 Answers

You can try using concatenate (numpy documentation):

flatten_x = np.concatenate(x)
like image 92
student Avatar answered Nov 15 '22 04:11

student