0

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

2
  • What i your input and expected output? What is wrong with the code you tried? Your question is extremely unclear and your example can't compile as it stands. Commented Jul 5, 2016 at 13:23
  • You've got rows * columns * depth cells. There's no need to iterate so deep, you already browse all the cells with your three first for loops. Commented Jul 5, 2016 at 13:25

3 Answers 3

3

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[].

Sign up to request clarification or add additional context in comments.

1 Comment

Nice Java 8 way. Is there a way how to specify a different order of indices, though?
1

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];
        }
    }
}

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.