xxxxxxxxxx
import numpy as np
twoDAry = [6.4],[5.9],[5.5],[5.3],[5.1],[4.9],[4.5],[4.5],[4.5],[4.3],[4.2],[3.8],[3.5],[2.8],[2.8],[2.8]
twoDAry = np.array(twoDAry)
print(twoDAry.reshape(1, -1)[0])
# Output
# [6.4 5.9 5.5 5.3 5.1 4.9 4.5 4.5 4.5 4.3 4.2 3.8 3.5 2.8 2.8 2.8]
xxxxxxxxxx
import itertools
a = [[1, 2], [3, 4], [5, 6]]
list(itertools.chain.from_iterable(a))
Output:- [1, 2, 3, 4, 5, 6]
xxxxxxxxxx
grid1D[row*col];
grid2D[row][col];
for(int i = 0; i < row; ++i)
for(int j = 0; j < col; ++j)
grid1D[i * col + j] = grid2D[i][j];
xxxxxxxxxx
# Python code to demonstrate
# flattening a 2d numpy array
# into 1d array
import numpy as np
ini_array1 = np.array([[1, 2, 3], [2, 4, 5], [1, 2, 3]])
# printing initial arrays
print("initial array", str(ini_array1))
# Multiplying arrays
result = ini_array1.flatten()
# printing result
print("New resulting array: ", result)
xxxxxxxxxx
# 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)
xxxxxxxxxx
arr2d = [[arr1d[i+j*width] for i in range(width)] for j in range(height)]
xxxxxxxxxx
int array[width * height];
int SetElement(int row, int col, int value)
{
array[width * row + col] = value;
}
xxxxxxxxxx
>>> tp = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> tp.ndim
1
>>> tp.shape
(10,)
>>> fp = np.array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
>>> fp.ndim
1
>>> fp.shape
(10,)
>>> combined = np.vstack((tp, fp)).T
>>> combined
array([[ 0, 10],
[ 1, 11],
[ 2, 12],
[ 3, 13],
[ 4, 14],
[ 5, 15],
[ 6, 16],
[ 7, 17],
[ 8, 18],
[ 9, 19]])
>>> combined.ndim
2
>>> combined.shape
(10, 2)
xxxxxxxxxx
#include <iostream>
using namespace std;
int main(){
int _2D_arr[3][3] = { {1,2,3} , {4,5,6} , {7,8,9} };
int _1D_arr[100]={1,2};
for (int i = 0; i < 3 * 3; i++) {
*(_1D_arr + i) = *(*_2D_arr + i);
cout << "1D arrr[" << i << "] " << _1D_arr[i] << endl;
}
}