-1

Hello and Happy new Year to all guys.

I came from world of matlab, and before one year started to code in java. I'm in countering problem that might be easy but bear with me please! here's the code of matlab: state is a variable of 3d array which its size: 2x4x64. state value is:(that's what I get on the editor of matlab)

state(:,:,1) =

     1     0     0     0
     2     0     1     1


state(:,:,2) =

     3     0     1     0
     4     0     0     1


state(:,:,3) =

     5     0     1     1
     6     0     0     0


state(:,:,4) =

     7     0     0     1
     8     0     1     0


state(:,:,5) =

     9     0     1     1
    10     0     0     0


state(:,:,6) =

    11     0     0     1
    12     0     1     0


state(:,:,7) =

    13     0     0     0
    14     0     1     1


state(:,:,8) =

    15     0     1     0
    16     0     0     1


state(:,:,9) =

    17     0     0     0
    18     0     1     1


state(:,:,10) =

    19     0     1     0
    20     0     0     1


state(:,:,11) =

    21     0     1     1
    22     0     0     0


state(:,:,12) =

    23     0     0     1
    24     0     1     0


state(:,:,13) =

    25     0     1     1
    26     0     0     0


state(:,:,14) =

    27     0     0     1
    28     0     1     0


state(:,:,15) =

    29     0     0     0
    30     0     1     1


state(:,:,16) =

    31     0     1     0
    32     0     0     1


state(:,:,17) =

    33     0     0     1
    34     0     1     0


state(:,:,18) =

    35     0     1     1
    36     0     0     0


state(:,:,19) =

    37     0     1     0
    38     0     0     1


state(:,:,20) =

    39     0     0     0
    40     0     1     1


state(:,:,21) =

    41     0     1     0
    42     0     0     1


state(:,:,22) =

    43     0     0     0
    44     0     1     1


state(:,:,23) =

    45     0     0     1
    46     0     1     0


state(:,:,24) =

    47     0     1     1
    48     0     0     0


state(:,:,25) =

    49     0     0     1
    50     0     1     0

state(:,:,26) =

    51     0     1     1
    52     0     0     0


state(:,:,27) =

    53     0     1     0
    54     0     0     1


state(:,:,28) =

    55     0     0     0
    56     0     1     1


state(:,:,29) =

    57     0     1     0
    58     0     0     1


state(:,:,30) =

    59     0     0     0
    60     0     1     1


state(:,:,31) =

    61     0     0     1
    62     0     1     0


state(:,:,32) =

    63     0     1     1
    64     0     0     0


state(:,:,33) =

     1     1     1     1
     2     1     0     0


state(:,:,34) =

     3     1     0     1
     4     1     1     0


state(:,:,35) =

     5     1     0     0
     6     1     1     1


state(:,:,36) =

     7     1     1     0
     8     1     0     1


state(:,:,37) =

     9     1     0     0
    10     1     1     1


state(:,:,38) =

    11     1     1     0
    12     1     0     1


state(:,:,39) =

    13     1     1     1
    14     1     0     0


state(:,:,40) =

    15     1     0     1
    16     1     1     0


state(:,:,41) =

    17     1     1     1
    18     1     0     0


state(:,:,42) =

    19     1     0     1
    20     1     1     0


state(:,:,43) =

    21     1     0     0
    22     1     1     1


state(:,:,44) =

    23     1     1     0
    24     1     0     1


state(:,:,45) =

    25     1     0     0
    26     1     1     1


state(:,:,46) =

    27     1     1     0
    28     1     0     1


state(:,:,47) =

    29     1     1     1
    30     1     0     0


state(:,:,48) =

    31     1     0     1
    32     1     1     0


state(:,:,49) =

    33     1     1     0
    34     1     0     1


state(:,:,50) =

    35     1     0     0
    36     1     1     1


state(:,:,51) =

    37     1     0     1
    38     1     1     0


state(:,:,52) =

    39     1     1     1
    40     1     0     0


state(:,:,53) =

    41     1     0     1
    42     1     1     0


state(:,:,54) =

    43     1     1     1
    44     1     0     0


state(:,:,55) =

    45     1     1     0
    46     1     0     1


state(:,:,56) =

    47     1     0     0
    48     1     1     1


state(:,:,57) =

    49     1     1     0
    50     1     0     1


state(:,:,58) =

    51     1     0     0
    52     1     1     1


state(:,:,59) =

    53     1     0     1
    54     1     1     0


state(:,:,60) =

    55     1     1     1
    56     1     0     0


state(:,:,61) =

    57     1     0     1
    58     1     1     0


state(:,:,62) =

    59     1     1     1
    60     1     0     0


state(:,:,63) =

    61     1     1     0
    62     1     0     1


state(:,:,64) =

    63     1     0     0
    64     1     1     1
%%%%%%%%%%%%%%%%%%%

revealed =[1 0];
tmp=[0,0];
for m=1:2
tmp[1]=sum(abs(state(l, 3:4 ,j)-revealed)) % here  state(m, 3:4 ,j) is actually the 3rd and 4th columns substracting from them the array revealed at every row (m=1, 2) of given j.
end
so at the end it returns tmp.

to who doesn't understand matlab is that there's an array state with size 2x4x64 and state(l, 3:4 ,j) gives me the two columns values according to j and then we subtract the array revealed from then and we do afterwards abs and then sum to the array result. for example: state (m, 3:4 , 1) . J=1 gives me [0 0] and [1 1] for m=1 , m=2 at third and 4th columns. so at every m I subtract revealed [1 0] from the two value of third columns and 4th columns values according to m. so here at m=1 we have [0 0] so [0 0] - revealed => [0 0]-[1 0] => [-1 0] and we do abs([-1 0]) so we get [1 0] and then we do sum which it's 1+0 => 1. At m=1 we get [1 1] so [1 1]-[1 0] => [1 0] we do abs on this array => [1 0] and then afterwards we do sum 1+0 => 1 .

I'm trying to do the same thing / same concept in java (to convert this code matlab to java) . so what I've done is this:

int[64][2][4] state=
{
{{ 1    , 0 ,    0 ,    0},
 { 2    , 0   ,  1,     1 }},

    {{ 3 ,    0 , 1 ,    0},
    {4   ,  0  ,   0  ,   1}},

    {{  5   ,  0 ,    1  ,   1},
    {6   ,  0 ,    0    , 0}},

    {{ 7    , 0   ,  0    , 1},
    { 8 ,    0  ,   1 ,    0 }}


   { {9     ,0  ,   1   ,  1},
    {10  ,   0   ,  0    , 0}},

    { { 11  ,   0   ,  0 ,    1},
      {12   ,  0   ,  1  ,   0}},




    { { 13  ,   0  ,   0  ,  0},
    {14   ,  0    , 1  ,   1}},




    { { 15  ,   0   ,  1 ,    0},
    { 16  ,   0  ,   0  ,   1} },


    {{17  ,   0  ,   0  ,   0},
    {18  ,   0  ,   1 ,    1 }},

    {{19  ,   0  ,   1   ,  0},
     {20  ,   0  ,   0  ,   1}},

    {{21   ,  0  ,   1 ,    1},
    { 22   ,  0   ,  0 ,    0}},


    { { 23 ,    0 ,    0,     1} ,
    {24  ,   0  ,   1  ,   0}},


    { {25  ,   0  ,   1 ,    1},
    {26   ,  0   ,  0  ,   0}},


    { {27   ,  0  ,   0 ,    1},
      {28  ,   0  ,   1  ,   0}},

    {{29   ,  0  ,   0  ,   0},
     {30  ,   0 ,    1 ,    1}},

    {{ 31   ,  0    , 1  ,   0},
     {32   ,  0  ,  0   ,  1}},


    { { 33 ,    0   ,  0 ,    1},
    {34   ,  0  ,   1 ,    0} },

    {{35  ,   0   ,  1   ,  1},
    {36  ,   0    , 0   ,  0}},

    {{ 37  ,   0  ,   1 ,    0},
     {38   ,  0  ,   0  ,   1}},

    {{39   ,  0  ,   0  ,   0},
     { 40   ,  0  ,   1  ,   1}},


    {{41  ,   0   ,  1 ,    0},
    { 42  ,   0  ,   0  ,   1}},


    {{ 43  ,   0  ,   0 ,    0},
     { 44  ,  0   ,  1  ,   1}}


    {45 ,    0  ,   0  ,   1},
    {46  ,   0  ,   1  ,   0} },


   { { 47   ,  0 ,    1  ,   1 },
     { 48   ,  0  ,   0  ,   0 } },


    {{ 49  ,   0  ,   0  ,   1 },
     { 50  ,   0   ,  1   ,  0 }},




  { { 51   ,  0    , 1 ,    1 },
    {52   ,  0   , 0  ,  0} },



    { { 53  ,   0,     1  ,   0 },
      {54   ,  0 ,   0  ,  1} },



    { { 55  ,   0   ,  0   ,  0 },
      { 56  ,   0   ,  1  ,   1} },


   { {57  ,   0  ,   1  ,   0},
     {58  ,   0  ,   0  ,   1} },



   { { 59   ,  0   ,  0 ,    0 },
      { 60   ,  0   ,  1  ,   1} },



   { { 61 ,    0 ,    0  ,   1 },
     { 62   ,  0  ,   1  ,   0} },



  { { 63   ,  0  ,   1 ,   1},
    { 64  ,  0  ,   0 ,   0 } },
};



 public class GlobalMember
    {
     int[] revealed =[1 0];
     int[] tmp= {0,0};
   for(int j=0 ; j<64 ; j++)  
     { for (int m = 0 ; l < 2 ; l++)
       {      
           tmp[l] = 
             IntStream.of((Math.abs(state[m, 3 : 4, j] - revealed))).sum();
             //note that I always just check 3rd and 4th columns values which 
            //they are implicitly an array with two element at every row of 
           //the two rows at every j(j is range from 0 to 64).
       }
     }
    }

but it sounds Im missing something and it doesn't compile at all, could anyone please help me how do I implement this code matlab in java? I mean to implement the same concept of matlab code in java.

Im struggling this about one week and I didn't understand where's exactly the problem.

thanks alot.

2 Answers2

0

you have to fix the initialization sintax (comma separated values), something similar was asked here Syntax for creating a two-dimensional array

I guess you should do something like this:

int[64][2][4] state=
{
{{1,0,0,0},
{2,0,1,1}},
{{3,0,1,0},
...
micciaLabs
  • 21
  • 3
0

First lets get familiar with the basics. let's start with 2D arrays.

a) 2D Arrays

Imagine 2D arrays not as an grid of numbers but as layers of 1D array's because each of these layers can have an 1D array of any length,lets look at an example

 //This 2D array has 3 layers, each layer is denoted using {} brackets

 int[][] test={
               {1,2}            //Layer 0 has 2 elements
              ,{3,4,5}          //Layer 1 has 3 elements  
              ,{6,7,8,9}        //Layer 2 has 4 elements
             };

To query this code you would do like this

int layers=test.length;  //This gives us the total layers in the 2D array
System.out.println(layers);   
for(int i=0;i<layers;i++){System.out.println(test[i].length);}//This queries the number of elements in each layer 

Output:

3  //Number of layers=3
2  //Layer 0 has 2 elements
3  //Layer 1 has 3 elements
4  //Layer 2 has 4 elements

You can also initialize an 2D array like this

int rows=2;
int cols=3;
int[][] test=new int[rows][cols];

However one limitation is you would have guessed it from above is that you can initialize your 2D array only in an grid fashion. The number of layers is the rows and the length of each 1D array in each layer is the columns.

To iterate over each element you would do it like this

for(int layer=0;layer<test.length;layer++)  //iterate over each layer
{
 for(int i=0;i<test[layer].length;i++)      //for each layer iterate over all elements in that layer
 {
  int element=test[layer][i];
 }
}

b) 3D Arrays

Now just as 2D arrays contain multiple layers of 1D arrays 3D arrays contains multiple layers of 2D arrays, let's look at an example

 int[][][] test=
  {
   {                     //This is layer 0
     {0,1}
    ,{2,3,4}
   }      
   ,     
   {                     //This is layer 1
     {5,6}
    ,{7,8,9}
    ,{10,11,12}
   }     
   ,      
   {                      //This is layer 2
     {13,14}
    ,{15,16,17}
    ,{18,19,20,21}
    ,{22,23,24,25,26}
   }
  };

Again you can also initialize an 3D array like this

int layers=3;
int rows=4;
int col=5;
int[][][] test=new int[layers][rows][col];

With again the limitation that every 2D array layer must be initialized in an grid fashion

In fact you can reference an 2D array from any layer in this 3D array like this

int[][] test2=test[0]; //get the 2D array stored in layer 0 

also to loop over every element in this 3D array you have dig through all the layers

for(int layer2D=0;layer2D<test.length;layer2D++)  //Iterate over every 2D array layer in this 3D array
{
 for(int layer1D=0;layer1D<test[layer2D].length;layer1D++)  //iterate over every 1D layer of this current 2D array
 {
  for(int element=0;element<test[layer2D][layer1D].length;element++) //finally iterate over every element of this current 1D layer or 1D array
  {
   int value=test[layer2D][layer1D][element];
    
  }
 }
}

Ok after all of that let's address your problem now.

1) Fix your array

Since you are initializing your 3D array using approach one you don't need to specify the lengths. Here is your final array

int[][][] state=              //No need to specify lengths just use empty brackets
  {
   {                          //Layer 0
    {1    , 0 ,    0 ,    0},
    {2    , 0   ,  1,     1 }
   }
   ,
   {                          //Layer 1 and so on
    {3 ,    0 , 1 ,    0},
    {4   ,  0  ,   0  ,   1}
   }
   ,
   {
    {5   ,  0 ,    1  ,   1},
    {6   ,  0 ,    0    , 0}
   }
   ,
   {
    {7    , 0   ,  0    , 1},
    {8 ,    0  ,   1 ,    0 }
   }
   ,
   { 
    {9     ,0  ,   1   ,  1},
    {10  ,   0   ,  0    , 0}
   }
   ,
   { 
    {11  ,   0   ,  0 ,    1},
    {12   ,  0   ,  1  ,   0}
   }
   ,
   { 
    {13  ,   0  ,   0  ,  0},
    {14   ,  0    , 1  ,   1}
   }
   ,
   { 
    {15  ,   0   ,  1 ,    0},
    {16  ,   0  ,   0  ,   1} 
   }
   ,
   {
    {17  ,   0  ,   0  ,   0},
    {18  ,   0  ,   1 ,    1 }
   }
   ,
   {
    {19  ,   0  ,   1   ,  0},
    {20  ,   0  ,   0  ,   1}
   }
  ,
  {
   {21   ,  0  ,   1 ,    1},
   {22   ,  0   ,  0 ,    0}
  }
  ,
  { 
   {23 ,    0 ,    0,     1},
   {24  ,   0  ,   1  ,   0}
  }
  ,
  { 
   {25  ,   0  ,   1 ,    1},
   {26   ,  0   ,  0  ,   0}
  }
  ,
  { 
   {27   ,  0  ,   0 ,    1},
   {28  ,   0  ,   1  ,   0}
  }
  ,
  {
   {29   ,  0  ,   0  ,   0},
   {30  ,   0 ,    1 ,    1}
  }
  ,
  {
   {31   ,  0    , 1  ,   0},
   {32   ,  0  ,  0   ,  1}
  }
  ,
  { 
   {33 ,    0   ,  0 ,    1},
   {34   ,  0  ,   1 ,    0} 
  }
  ,
  {
   {35  ,   0   ,  1   ,  1},
   {36  ,   0    , 0   ,  0}
  }
  ,
  {
   {37  ,   0  ,   1 ,    0},
   {38   ,  0  ,   0  ,   1}
  }
  ,
  {
   {39   ,  0  ,   0  ,   0},
   {40   ,  0  ,   1  ,   1}
  }
  ,
  {
   {41  ,   0   ,  1 ,    0},
   {42  ,   0  ,   0  ,   1}
  }
  ,
  {
   {43  ,   0  ,   0 ,    0},
   {44  ,  0   ,  1  ,   1}
  }
  ,
  {
   {45 ,    0  ,   0  ,   1},
   {46  ,   0  ,   1  ,   0} 
  }
  ,
  { 
   {47   ,  0 ,    1  ,   1 },
   {48   ,  0  ,   0  ,   0 } 
  }
  ,
  {
   { 49  ,   0  ,   0  ,   1 },
   { 50  ,   0   ,  1   ,  0 }
  }
  ,
  { 
   {51   ,  0    , 1 ,    1 },
   {52   ,  0   , 0  ,  0} 
  }
  ,
  { 
   {53  ,   0,     1  ,   0 },
   {54   ,  0 ,   0  ,  1} 
  }
  ,
  { 
   {55  ,   0   ,  0   ,  0 },
   {56  ,   0   ,  1  ,   1} 
  }
  ,
  { 
   {57  ,   0  ,   1  ,   0},
   {58  ,   0  ,   0  ,   1} 
  }
  ,
  { 
   {59   ,  0   ,  0 ,    0 },
   {60   ,  0   ,  1  ,   1} 
  }
  ,
  { 
   {61 ,    0 ,    0  ,   1 },
   {62   ,  0  ,   1  ,   0}
  }
  ,
  { 
   {63   ,  0  ,   1 ,   1},
   {64  ,  0  ,   0 ,   0 } 
  }
  };
  1. Fix Your code

This code right here

public class GlobalMember
    {
     int[] revealed =[1 0];
     int[] tmp= {0,0};
   for(int j=0 ; j<64 ; j++)  
     { for (int m = 0 ; l < 2 ; l++)
       {      
           tmp[l] = 
             IntStream.of((Math.abs(state[m, 3 : 4, j] - revealed))).sum();
             //note that I always just check 3rd and 4th columns values which 
            //they are implicitly an array with two element at every row of 
           //the two rows at every j(j is range from 0 to 64).
       }
     }
    }

Won't do you see every program in java begins with a class and an main method and so everything you do needs to encapsulated in an method which is further inside an class you can read this simple tutorial

Let's put that in an method and forgive me if i don't understand your logic but basically if you are trying to find the sum of all elements you have to declare an variable to store the result and keep adding to it to get your result. While we are at it let's declare your main method as well in this class

public class FindSum
{
 public static void main(String[] args)
 {
  int[][][] state=//The array i showed above cause i can't be asked to paste that here again

  System.out.println(findSum(state)); //You call that function here
 }

 public static int findSum(int[][][] state)
 {
  int sum=0;

  //Using the same logic from above

   for(int layer2D=0;layer2D<state.length;layer2D++)  //Iterate over every 2D array layer in this 3D array
   {
     for(int layer1D=0;layer1D<state[layer2D].length;layer1D++)  //iterate over every 1D layer of this current 2D array
     {
      for(int element=0;element<state[layer2D][layer1D].length;element++) //finally iterate over every element of this current 1D layer or 1D array
      {
       int value=state[layer2D][layer1D][element];
        
       sum+=value;
      }
     }
   }
   return sum;
 }
}

I have covered only the basics here So find some good tutorials and there a lot out there.

Sync it
  • 1,180
  • 2
  • 11
  • 29
  • I understand thank you much but you're misunderstanding me so bear with me please because Im struggling on this problem about two weeks. I want to add just the two elements of the 3rd and fourth columns for each row of the array (each four values of the array I take the last two values, there's 64 rows in the array). for first row for instance the value of 3rd and fourth columns of array is [0 0] , for second row the value of 3rd and fourth columns value are [1 1] ; so I need to return just the sum of 3rd and fourth values of the columns at each row. – RyanRonald Jan 06 '21 at 21:17
  • Then your problem becomes even simpler cause you don't need a 3d array but just a 2d one. Your inner loop of the 2d array(which I have shown how to iterate above) just needs to loop from 2->3[3rd and 4th column cause array indices start from 0->n-1] and then use the same sum logic which I have shown above. – Sync it Jan 07 '21 at 04:08