javamatrixbinaryinterlacing

Interlacing of two Binary Matrices


I have a 2x14 binary matrix P, which I have stored into two Matrices P1 and P2 of order 2x7. I need to interlace these two matrices P1 and P2, ie., the first element from P1 and the first element from P2 are taken and stored in to another matrix. When the first row of both the matrices is done I should get a new Matrix L1 of order 2x7.

The matrix I have taken is

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

This is P1 Matrix.

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

This is P2 Matrix.

After Interlacing the first row of both P1 and P2 the resultant matrix should be

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

This should be saved into L1 Matrix.

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

This should be saved into L2 Matrix.

I've tried doing it by saving the entire matrix into a single 2x14 matrix but I do not get the desired output.

This is the conversion of decimal numbers into bits and saving them into two 2x7 matrix.

 public static void main(String[] args) throws Exception {
    int[][] p = {{1, 2}, {3, 4}};
    int[][] P = new int[2][14];
    int[][] P1 = new int[2][7];
    int[][] P2 = new int[2][7];
    int[][] L1 = new int[2][14];
    for (int r = 0; r < 2; ++r) {
        for (int c = 0; c < 14; ++c) {
            P[r][c] = (p[r][c / 7] >> (7 - c % 7 - 1)) & 1;
            System.out.print(" " + P[r][c]);
        }
        System.out.println();
    }

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 7; j++) {
            P1[i][j] = P[i][j];
        }
    }
    for (int i = 0; i < 2; i++) {
        for (int j = 7; j < 14; j++) {
            P2[i][j - 7] = P[i][j];
        }
    }
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 7; j++) {
            System.out.print(" "+P1[i][j]);
        }
        System.out.println();
    }
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 7; j++) {
            System.out.print(" "+P2[i][j]);
        }
        System.out.println();
    }

The interlacing part that I have tried is.

for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 14; j+=2) {
            if(i==1&&j==0)
                ;
            else
                L1[i][j] = P1[i][j/2];

            if(j/2==3)
                L1[1][0] = P2[i][j/2];
            else
                L1[i][j+1] = P2[i][j/2];
        }
    }
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 14; j++) {
            System.out.print(" " + L1[i][j]);
        }
        System.out.println();
    }

For this I get the output as

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

The part which is bold should be in the second row of the matrix.


Solution

  • I figured out how to solve this.

    Here is what I have done.

    public class BinaryConv {
    
    public static void main(String[] args) throws Exception {
        int[][] p = {{1, 2}, {3, 4}};
        int[][] P = new int[2][14];
        int[][] P1 = new int[2][7];
        int[][] P2 = new int[2][7];
        int[] L1 = new int[28];
        for (int r = 0; r < 2; ++r) 
        {
            for (int c = 0; c < 14; ++c) 
            {
                P[r][c] = (p[r][c / 7] >> (7 - c % 7 - 1)) & 1;
                System.out.print(" " + P[r][c]);
            }
            System.out.println();
        }
    
        for (int i = 0; i < 2; i++) 
        {
            for (int j = 0; j < 7; j++) 
            {
                P1[i][j] = P[i][j];
            }
        }
    
        for (int i = 0; i < 2; i++) 
        {
            for (int j = 7; j < 14; j++) 
            {
                P2[i][j - 7] = P[i][j];
            }
        }
        System.out.println("The sub matrix 'P1' is");
        for (int i = 0; i < 2; i++) 
        {
            for (int j = 0; j < 7; j++) 
            {
                System.out.print(" "+P1[i][j]);
            }
            System.out.println();
        }
        System.out.println("The sub matrix 'P2' is");
        for (int i = 0; i < 2; i++) 
        {
            for (int j = 0; j < 7; j++) 
            {
                System.out.print(" "+P2[i][j]);
            }
            System.out.println();
        }
        int l=0;
        for (int i = 0; i < 2; i++) 
        {
            for (int j = 0; j < 7; j++) 
            {
                    L1[l] = P1[i][j];
                    L1[l+1] = P2[i][j]; 
                    l+=2;
            }
        }
        int k =0;
        for (int i = 0; i < 2; i++) 
        {
            for(int j=0;j<7;j++)
            {
                P1[i][j] = L1[k];
                P2[i][j] = L1[k+14];
                k++;
        }
    
    }
        System.out.println("The interlaced binary matrix 'P1' is");
        for (int i = 0; i < 2; i++) 
        {
            for (int j = 0; j < 7; j++) 
            {
                System.out.print(" "+P1[i][j]);
            }
            System.out.println();
        }
        System.out.println("The interlaced binary matrix 'P2' is");
        for (int i = 0; i < 2; i++) 
        {
            for (int j = 0; j < 7; j++) 
            {
                System.out.print(" "+P2[i][j]);
            }
            System.out.println();
        }
    }
    

    }

    The output looks like this

    0 0 0 0 0 0 1 0 0 0 0 0 1 0
    0 0 0 0 0 1 1 0 0 0 0 1 0 0
    The sub matrix 'P1' is
    0 0 0 0 0 0 1
    0 0 0 0 0 1 1
    The sub matrix 'P2' is
    0 0 0 0 0 1 0
    0 0 0 0 1 0 0
    The interlaced binary matrix 'P1' is
    0 0 0 0 0 0 0
    0 0 0 0 1 1 0
    The interlaced binary matrix 'P2' is
    0 0 0 0 0 0 0
    0 0 1 1 0 1 0