remap array to 2d array

Hey erryone. I looking for an equation or two where i would be able to input a number, maybe from 0-24, and then get the x and y coordinates for a 5x5 matrix. if read a bunch of posts, but they all seem to use for loops, but that seems time consuming for my project.

I would like them mapped like this;

    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
Tagged:

Answers

  • Let's make a matrix of 3x6 instead of 5x5 in order to be able to identify the dimensions directly, so 3 rows and 6 columns.

    You have an input number named val. We know that val is constrained between 0 and 3*6-1. Then to get the column you do col=val%6 and similarly for rows, row=int(val/6).

    int val=18;
    int ncol=6;
    
    for(int i=0;i<18;i++) 
      println("row "+int(i/ncol)+"\tCol "+i%ncol);
    

    Kf

  • int val=18;
    int ncol=5;
    
    
    void setup() {
      for (int i=0; i<25; i++) { 
    
        PVectorInt result1=getGridPosition(i, ncol); 
    
        println(i+":\t    row "+result1.x
          +"\tCol "+result1.y);
      }
    }
    
    PVectorInt getGridPosition( int i, int ncol ) {
      PVectorInt result = new PVectorInt(); 
      result.x=int(i/ncol); 
      result.y=i%ncol;
      return result;
    }
    
    // ======================================
    
    class PVectorInt {
      int x, y;
    }
    
Sign In or Register to comment.