Loading...
Logo
Processing Forum
hi guys,

i have a set of 2d arrays which i've stored in an arraylist. How do i combine the 2d arrays?

the script is a bit lengthy so i dont want to post the whole thing up...

Copy code
  1.  //xs is the int[][] arraylist
  2. //finalSum is a variable stored in the class
  3.     for(int i=0;i<xs.size();i++){
  4.       int valval[][] = new int[imgwidth][imgheight];
  5.       valval =  xs.get(i);
  6.       for(int j=0;j<imgwidth;j++){
  7.       for(int k=0;k<imgheight;k++){
  8.         
  9.       finalSum[j][k] = finalSum[j][k] + valval[j][k];
  10.       }
  11.       }
  12.     }

Replies(3)

hi guys i'm still pretty stuck, what is the best way of combining 2d arrays?

eg: 

array1 {[1,0,0],
           [0,1,0],
           [0,0,1]}

array2 {[1,0,0],
           [0,1,0],
           [0,0,1]}

array1 + array 2 = {[2,0,0],
                             [0,2,0],
                             [0,0,2]}
You create an array then drop it. Just declare it:

Copy code
  1.     for(int i=0;i<xs.size();i++){
  2.       int valval[][] = xs.get(i);
  3.       for(int j=0;j<imgwidth;j++){
  4.         for(int k=0;k<imgheight;k++){
  5.           finalSum[j][k] += valval[j][k];
  6.         }
  7.       }
  8.     }
What is wrong with that? (I made minor improvements, not changed the result.)
thanks phi, seems to be solved. I needed to store each value in a class for it to work.

i seem to be having some out of memory issues now though. I ended up storing this in a function within a class

do you have any advice on which parts could be only called once? i seem to be getting an out of memory issue. apologies for the toxilibs Vec3D library in this part of the forum.

Copy code
  1. class agentInput {
  2.   //input agent
  3.   //output value[][] with mod
  4.   Vec3D pos;
  5.   int outputNest[][] = new int[imgwidth][imgheight];
  6.   int updateVal[][]=new int[imgwidth][imgheight];

  7.   agentInput(Vec3D _pos) {
  8.     pos = _pos;
  9.   }




  10.   void softSelect() {
  11.     for (int x=0;x<imgwidth;x++) {
  12.       for (int y=0;y<imgheight;y++) {        
  13.         Vec3D otherPoints = new Vec3D(x, y, pos.z);
  14.         float distance = pos.distanceTo(otherPoints);
  15.         if (distance>1 && distance <20) {
  16.           outputNest[int(otherPoints.x)][int(otherPoints.y)] = int(30*gridSize/pow(distance, 2));
  17.         }
  18.       }
  19.     }
  20.   }

  21.   int[][] outputNest(int output[][]) {

  22.     for (int x=0;x<imgwidth;x++) {
  23.       for (int y=0;y<imgheight;y++) {
  24.         output[x][y] = outputNest[x][y];
  25.       }
  26.     }

  27.     return output;
  28.   }
  29.   
  30.   int[][] combineNest(int sum1[][], int result[][]){
  31.   
  32.       for(int i=0;i<imgwidth;i++){
  33.     for(int j=0;j<imgheight;j++){
  34.       result[i][j]+=sum1[i][j];
  35.     }
  36.       }
  37.     
  38.   return result;
  39.   
  40.   }
  41. }