我有一个2x14二进制矩阵P,我已将其存储到2x7阶的两个矩阵P1和P2中。我需要交织这两个矩阵P1和P2,即,来自P1的第一个元素和来自P2的第一个元素被取出并存储到另一个矩阵中。当两个矩阵的第一行完成时,我应该得到一个2x7阶的新矩阵L1。
我采取的矩阵是
0 0 0 0 0 0 1
0 0 0 0 0 1 1
这是P1 Matrix。
0 0 0 0 0 1 0
0 0 0 0 1 0 0
这是P2 Matrix。
在交错P1和P2的第一行之后,得到的矩阵应该是
0 0 0 0 0 0 0
0 0 0 0 1 1 0
这应保存在L1 Matrix中。
0 0 0 0 0 0 0
0 0 1 1 0 1 0
这应保存在L2 Matrix中。
我已尝试将整个矩阵保存到单个2x14矩阵中,但我没有得到所需的输出。
这是将十进制数转换为位并将它们保存为两个2x7矩阵。
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();
}
我尝试过的隔行扫描部分是。
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();
}
为此我输出
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
粗体部分应位于矩阵的第二行。
答案 0 :(得分:1)
我想出了如何解决这个问题。
这就是我所做的。
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();
}
}
}
输出看起来像这样
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
子矩阵&#39; P1&#39;是
0 0 0 0 0 0 1
0 0 0 0 0 1 1
子矩阵&#39; P2&#39;是
0 0 0 0 0 1 0
0 0 0 0 1 0 0
隔行扫描二进制矩阵&#39; P1&#39;是
0 0 0 0 0 0 0
0 0 0 0 1 1 0
隔行扫描二进制矩阵&#39; P2&#39;是
0 0 0 0 0 0 0
0 0 1 1 0 1 0