python 1d array into 2d array
arr2d = [[arr1d[i+j*width] for i in range(width)] for j in range(height)]
python 1d array into 2d array
arr2d = [[arr1d[i+j*width] for i in range(width)] for j in range(height)]
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()
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)
array 2d to 1d
int array[width * height];
int SetElement(int row, int col, int value)
{
array[width * row + col] = value;
}
c program to represent 2d matrix in 1d matrix
#include<stdio.h>
#define n 3
int main()
{
int a[n][n],b[n*n],c[n*n],i,j,k=0,l=0;
printf(“\n Enter elements of 2D array : “);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“\n Given 2D array : \n“);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf(“%d ”,a[i][j]);
}
printf(“\n”);
}
printf(“\n Row wise \n”);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
b[k]=a[i][j];
k++;
}
}
printf(“\n Column wise \n”);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c[l]=a[j][i];
l++;
}
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us