change 1d array to 2d array python code example
Example 1: array 2d to 1d
int array[width * height];
int SetElement(int row, int col, int value)
{
array[width * row + col] = value;
}
Example 2: numpy convert 1d array to 2d
import numpy as np
one_dim_arr = np.array([1, 2, 3, 4, 5, 6])
two_dim_arr = one_dim_arr[np.newaxis, :]
print(two_dim_arr)
print()
two_dim_arr = one_dim_arr[:,np.newaxis]
print(two_dim_arr)
print()
two_dim_arr = np.expand_dims(one_dim_arr,axis = 0)
print(two_dim_arr)
print()
two_dim_arr = np.expand_dims(one_dim_arr,axis = 1)
print(two_dim_arr)