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;
}
}
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
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
# 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
int array[width * height];
int SetElement(int row, int col, int value)
{
array[width * row + col] = value;
}