java - Scaling An Array (Matrix) -


the intention of program create larger array of bytes scaled factor of 10 original array. example, 1 in [0][0] should 10x10 square of 1's in new array. provide code , output, seems work during population of larger array, prints different values. i'm experimenting rows in order limit number of variables i'm dealing during testing. can think of reason why happens?

public class test  { static byte[][] bytearray = {{1, 0},  {0, 1}};  public static void main(string[] args) {     byte newarray[][] = converter();     for(int = 0; < 20; i++)     {         system.out.println(newarray[i][0]);     } }  private static byte[][] converter() {     byte[][] b = new byte[20][20];      for(int r = 0; r < 2; r++)     {         for(int = 0; < 10; i++)         {             b[r+i][0] = bytearray[r][0];             system.out.println(bytearray[r][0]);             system.out.println(b[r+i][0]);         }     }      return b; } 

}

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

why not use truncated integer division advantage:

static void printmat(byte[][] mat)  // utility function print matrix {      for(byte[] row : mat)     {         system.out.println(arrays.tostring(row));     } }  private static byte[][] stretch(byte[][] bytes, int rfactor, int cfactor) // stretch matrix in 'bytes' //stretch rows 'rfactor' , columns 'cfactor' {      // create empty matrix:     int rows = bytes.length*rfactor; // rows in new matrix     int cols = bytes[0].length*cfactor; // columns in new matrix     byte[][] out = new byte[rows][cols]; // our new, stretched matrix      // loop through rows , columns of *new* matrix:     for(int r = 0; r < rows; r++)     {         for(int c = 0; c < cols; c++)         {             // divide row , column indices              // appropriate factors find correct value             // in original matrix.             // integer division drops remainder,             // want.             out[r][c] = bytes[r/rfactor][c/cfactor];         }     }     return out; }  public static void main(string[] args) throws exception  {     // example:     byte[][] bytearray =         {{1, 0},          {0, 1}};     byte[][] newarray = stretch(bytearray, 10, 10);     printmat(newarray);      system.out.println();      // can stretch matrix dimensions:     byte[][] bytearray2 =         {{1, 2, 3},          {4, 5, 6}};     byte[][] newarray2 = stretch(bytearray2, 3, 2);     printmat(newarray2);  } 

output:

[1, 1, 1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 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]  [1, 1, 2, 2, 3, 3] [1, 1, 2, 2, 3, 3] [1, 1, 2, 2, 3, 3] [4, 4, 5, 5, 6, 6] [4, 4, 5, 5, 6, 6] [4, 4, 5, 5, 6, 6] 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -