Loading...
Logo
Processing Forum
Hi all, so I want to convert a 3d array to 1d so that I can use sort () function to get the top10 largest numbers, is there a way to do it in Processing? 

Replies(5)

AFAIK, any programming language can do that!  It's not a Processing exclusive matter. 

It's the opposite I did in this post below (1D to 2D). But you need a 3D to 1D: 
http://forum.processing.org/topic/assistance-needed-converting-a-1d-array-to-a-2d-array

Here's my template solution:
Copy code
    // http://forum.processing.org/topic/convert-a-3d-array-to-1d
    
    final int D1 = 10, D2 = 3, D3 = 5;
    final int[][][] arr3d = new int[D1][D2][D3];
    
    final int D = D1*D2*D3;
    final int[] arr1d = new int[D];
    
    println(D + "\n");
    
    for (int x=0; x!=D1; ++x)  for (int y=0; y!=D2; ++y)  for (int z=0; z!=D3;) {
      final int idx = x*D2*D3 + y*D3 + z;
      arr1d[idx] = arr3d[x][y][z++];
      print(idx + " - ");
    }
    
    final int[] sorted = sort(arr1d);
    
    exit();
    
yes but how? 
like gotoloop did it

here is a lttle longer version


final int D1 = 10, D2 = 3, D3 = 5;
final int[][][] arr3d = new int[D1][D2][D3];

final int D = D1*D2*D3;
final int[] arr1d = new int[D];

println(D + "\n");

// fill 3D
for (int x=0; x!=D1; ++x) 
  for (int y=0; y!=D2; ++y) 
    for (int z=0; z!=D3;) {
      arr3d[x][y][z]=int(random (55));
      print(arr3d[x][y][z++] + " - ");
    }
println ();
println ();
// copy to 1D
for (int x=0; x!=D1; ++x) 
  for (int y=0; y!=D2; ++y) 
    for (int z=0; z!=D3;) {
      final int idx = x*D2*D3 + y*D3 + z;
      arr1d[idx] = arr3d[x][y][z++];
      print(idx + " - ");
    }
println ();
println ();
// sort
final int[] sorted = sort(arr1d);
// show sorted 1D
for (int x=0; x!=sorted.length; ++x)
  print (    sorted[x]  + " - " );
exit();





150

48 - 15 - 35 - 32 - 21 - 15 - 17 - 10 - 27 - 33 - 46 - 48 - 38 - 13 - 10 - 33 - 43 - 45 - 51 - 36 - 0 - 18 - 52 - 12 - 29 - 54 - 41 - 38 - 27 - 29 - 20 - 9 - 29 - 17 - 5 - 29 - 20 - 47 - 40 - 4 - 3 - 0 - 5 - 32 - 52 - 5 - 32 - 17 - 25 - 7 - 9 - 19 - 30 - 5 - 42 - 40 - 32 - 28 - 54 - 51 - 23 - 40 - 26 - 28 - 42 - 32 - 41 - 9 - 54 - 5 - 46 - 35 - 25 - 8 - 20 - 38 - 18 - 47 - 35 - 10 - 47 - 49 - 23 - 38 - 49 - 40 - 0 - 23 - 19 - 54 - 42 - 10 - 15 - 17 - 44 - 22 - 29 - 15 - 23 - 24 - 12 - 25 - 17 - 54 - 30 - 45 - 41 - 53 - 23 - 12 - 0 - 3 - 25 - 53 - 34 - 53 - 26 - 10 - 24 - 15 - 34 - 38 - 1 - 37 - 46 - 4 - 4 - 14 - 35 - 21 - 35 - 50 - 2 - 24 - 21 - 22 - 2 - 5 - 25 - 18 - 32 - 40 - 20 - 33 - 33 - 33 - 23 - 1 - 53 - 30 -

0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - 11 - 12 - 13 - 14 - 15 - 16 - 17 - 18 - 19 - 20 - 21 - 22 - 23 - 24 - 25 - 26 - 27 - 28 - 29 - 30 - 31 - 32 - 33 - 34 - 35 - 36 - 37 - 38 - 39 - 40 - 41 - 42 - 43 - 44 - 45 - 46 - 47 - 48 - 49 - 50 - 51 - 52 - 53 - 54 - 55 - 56 - 57 - 58 - 59 - 60 - 61 - 62 - 63 - 64 - 65 - 66 - 67 - 68 - 69 - 70 - 71 - 72 - 73 - 74 - 75 - 76 - 77 - 78 - 79 - 80 - 81 - 82 - 83 - 84 - 85 - 86 - 87 - 88 - 89 - 90 - 91 - 92 - 93 - 94 - 95 - 96 - 97 - 98 - 99 - 100 - 101 - 102 - 103 - 104 - 105 - 106 - 107 - 108 - 109 - 110 - 111 - 112 - 113 - 114 - 115 - 116 - 117 - 118 - 119 - 120 - 121 - 122 - 123 - 124 - 125 - 126 - 127 - 128 - 129 - 130 - 131 - 132 - 133 - 134 - 135 - 136 - 137 - 138 - 139 - 140 - 141 - 142 - 143 - 144 - 145 - 146 - 147 - 148 - 149 -

0 - 0 - 0 - 0 - 1 - 1 - 2 - 2 - 3 - 3 - 4 - 4 - 4 - 5 - 5 - 5 - 5 - 5 - 5 - 7 - 8 - 9 - 9 - 9 - 10 - 10 - 10 - 10 - 10 - 12 - 12 - 12 - 13 - 14 - 15 - 15 - 15 - 15 - 15 - 17 - 17 - 17 - 17 - 17 - 18 - 18 - 18 - 19 - 19 - 20 - 20 - 20 - 20 - 21 - 21 - 21 - 22 - 22 - 23 - 23 - 23 - 23 - 23 - 23 - 24 - 24 - 24 - 25 - 25 - 25 - 25 - 25 - 26 - 26 - 27 - 27 - 28 - 28 - 29 - 29 - 29 - 29 - 29 - 30 - 30 - 30 - 32 - 32 - 32 - 32 - 32 - 32 - 33 - 33 - 33 - 33 - 33 - 34 - 34 - 35 - 35 - 35 - 35 - 35 - 36 - 37 - 38 - 38 - 38 - 38 - 38 - 40 - 40 - 40 - 40 - 40 - 41 - 41 - 41 - 42 - 42 - 42 - 43 - 44 - 45 - 45 - 46 - 46 - 46 - 47 - 47 - 47 - 48 - 48 - 49 - 49 - 50 - 51 - 51 - 52 - 52 - 53 - 53 - 53 - 53 - 54 - 54 - 54 - 54 - 54 -


Greetings, Chrisir

If you need an answer, please send me a personal message since this forum doesn't notify.
For completeness sake, line -> final int idx = x*D2*D3 + y*D3 + z; is just visually educative. 
We can safely replace it by a simple 1by1 counter:
Copy code
    for (int idx=0, x=0; x!=D1; ++x)  for (int y=0; y!=D2; ++y)  for (int z=0; z!=D3;) {
      //final int idx = x*D2*D3 + y*D3 + z;
      print(idx + " - ");
      arr1d[idx++] = arr3d[x][y][z++];
    }
    
    
Also, since we know the resultant sorted array is of the same length of its source,
we can use the constant used to create the later (D) in place of sorted.length

println('\n');
final int[] sorted = sort(arr1d);

for (int x=0; x!=D; print(sorted[x++]  + " - "));

exit();

Assuming you want to have regularly sized arrays ..... and don't see anything wrong with the array unrolling.

Copy code
  1. int[][][] a;
  2. int A=5;
  3. int B=6;
  4. int C=7;
  5. a=new int[A][B][C];
  6. int j=0,r=0,h=0,d=0;
  7. for(int i=0;i<A*B*C;i++,j=(i%A==0)?j+1:j,r=i-j*A,h=(i%A==0 && r==0 && h<B+1)?h+1:h,h=(h==B)?0:h,d=(i%(A*B)==0)?d+1:d){
  8.   a[r][h][d]=i;
  9. }

  10. int i=0;
  11. int[] result=new int[A*B*C];
  12. for(int[][] tx: a){
  13.  for(int[] ty:tx){
  14.    for(int tz:ty){
  15.      result[i]=tz;
  16.      i++;
  17.    }
  18.  }
  19. }
  20. result=sort(result);
  21. for(int p:result){
  22.  println("p "+p);
  23. }