简体   繁体   中英

How can i split an 1D array to 2D array with specific size of column?

I have a one dimensional array arr1D[i] and i want it to convert in two dimensional array arr2D[i][j] , where the size of column in 2D array should be 512 and row size can be any thing. For example, if i have 1D array with size arr1D[1024] then the corresponding 2D array should be arr2D[2][512] .

How about two forloops to copy them:

2darray[][];
size1d = 1024;
num_colums = 512;
num_rows = size1d/num_colums;
for(i = 0; i < num_rows; i++){
     for(j = 0; j < num_colums; j++){
          2darray[i][j] = 1darray[i*num_colums+j];
      }
 }

This is just pseudocode, but with a few tweeks it should work; hope it helps :-)

 int len=arr1D.length(); if(len%512 !=0)) len= len/512 +1; else len=len/512; int arr2D= new int[len][512]; int k=0; for(int i=0; i<len-1; i++) { for(int j=0; j<512; j++) { arr2D[i][j]=arr1D[k]; k++; if (k==arr1D.length()) break; } } 

Well you can easily create it by doing

yourtype arr2D[][] = new yourtype[arr1D.length/2][512] // yourtype :=[int,float,..]

Now all depends on you, how you are going to copy those elements of arr1d in arr2d

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM