简体   繁体   中英

Flatten 3D array to 1D array

How can we convert 3D array to 1D array in java??

I used the code bellow:

input :double  [][][]S_p = { { { 1.1, 2.1 }, { 3.2, 4.1 } },  
    { { 5.2, 6.1 }, { 7.1, 8.3 } } };

int rows = S_p.length;
int columns = S_p[0].length;
int depth = S_p[0][0].length;
double [] d1 = new double[row*columns*depth];

for(int i=0;i<depth;i++) {
    for(int j=0;j<rows;j++){
        for(int k=0;k<columns;k++) {         
            for(int ii=0;ii<rows*columns*depth;ii++) {
                d1 [ii]  = S_p[ depth *rows *i + columns *k +j];
            }
        }
    }

out put b[]= {1.1, 2.1, 3.2 , 4.1 ...}

But this does not work

In Java 8 you can simply do:

double[][][] vals = {{{1.1, 2.1}, {3.2, 4.1}}, {{5.2, 6.1}, {7.1, 8.3}}};

double[] test = Arrays.stream(vals)
                      .flatMap(Arrays::stream)
                      .flatMapToDouble(Arrays::stream)
                      .toArray();

System.out.println(Arrays.toString(test));

Output:

[1.1, 2.1, 3.2, 4.1, 5.2, 6.1, 7.1, 8.3]

Explanation:

Arrays.stream(vals) creates a Stream<double[][]> .

.flatMap(Arrays::stream) flattens it into a Stream<double[]>

.flatMapToDouble flattens the Stream<double[]> into an DoubleStream

Finally .toArray() collects all the values in the DoubleStream and returns a double[] .

Your method is correct, but you are not multiplying your coordinates correctly. A good way to make sure you're correct is to use an adaptation of Horner's scheme: value_x + upper_bound_of_x * (value_y + upper_bound_of_y * ( ... )) .

Also, the inner-most loop is superfluous, you should be able to calculate the index to S_p using the method above.

int rows = S_p.length;
int columns = S_p[0].length;
int depth = S_p[0][0].length;
double[] d1 = new double[rows * columns * depth];

for (int i = 0; i < depth; i++) {
    for (int j = 0; j < rows; j++) {
        for (int k = 0; k < columns; k++) {
            d1[i + depth*(j + rows*(k))] = S_p[j][k][i];
        }
    }
}

I was struggling with this problem for some time. Given a 1D array1D[height x width x depth] and 3D array array3D[height][width][depth] with x in height y in width and z in depth. the folowing loops maps correctly every element in array3D to array1D by the following equation:

 x* width + y +z*( width * height)

code it in C++:

 for(int x=0; x<height;++x){
     for (int y=0; y< width; ++y){   
         for (int z=0; z< depth; ++z){
             array3D[x][y][z]=array1D[x* width + y +z*( width * height)];}}}

if you are doing some calculations and you want to save your data in 1D array that will be converted later on to 3D array you are looking at:

 for(int x=0; x<height;++x){
     for (int y=0; y< width; ++y){   
         for (int z=0; z< depth; ++z){
             array1D[x* width + y +z*( width * height)]=YourCalculatedData;}}}

PS: for Matlab you should minus 1 from indices and at last you should add 1 to the equation because Matlab's loops start from 1, not 0

for x=1: height
    for y=1: width
        for z= 1:depth
            volume(x,y,z)=rawdata((x-1)* width + (y-1 ) +(z-1)*( width * height)+1);
        
        end
    end
end

Good luck!

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