Answers for "2d array to 1d array python"

C++
1

convert 2d list to 1d python

import itertools

a = [[1, 2], [3, 4], [5, 6]]
list(itertools.chain.from_iterable(a))

Output:- [1, 2, 3, 4, 5, 6]
Posted by: Guest on August-27-2021
0

2d to 1d array python

import numpy as np  
ini_array1 = np.array([[1, 2, 3], [2, 4, 5], [1, 2, 3]])
result = ini_array1.flatten()
Posted by: Guest on August-01-2021
-2

numpy convert 1d array to 2d

import numpy as np 

# 1D array 
one_dim_arr = np.array([1, 2, 3, 4, 5, 6])

# to convert to 2D array
# we can use the np.ndarray.reshape(shape) function 
# here shape is given by two integers seperated by a comma
# the two integers specify m,n for the new matrix 
# ensure that the matrix that you are trying to generate
# has a size that meets the number of elements in the 1D array. 
# for that make sure that 
# m * n = number of elements in the one dimentional array 

two_dim_arr = one_dim_arr.reshape(1, 6)

#which returns a 2D array
print(two_dim_arr)


# confirmed by the array.ndim attribute
print(two_dim_arr.ndim)

# you can even specify one of the dimensions as unknown by passing -1
# numpy will infer the length using the array and remaining dimensions

two_dim_arr = one_dim_arr.reshape(1,-1)
Posted by: Guest on November-14-2020
1

array 2d to 1d

int array[width * height];

 int SetElement(int row, int col, int value)
 {
    array[width * row + col] = value;  
 }
Posted by: Guest on January-28-2020
0

pass 2d array to 1d python

# Create a 2D Numpy Array.
arr = np. array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
# convert 2D array to a 1D array of size 9.
flat_arr = np. reshape(arr, 9)
Posted by: Guest on June-08-2021

Browse Popular Code Answers by Language