Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 缩放阵列(矩阵)_Java_Arrays_Matrix_Byte_Scaling - Fatal编程技术网

Java 缩放阵列(矩阵)

Java 缩放阵列(矩阵),java,arrays,matrix,byte,scaling,Java,Arrays,Matrix,Byte,Scaling,这个程序的目的是创建一个更大的字节数组,从原始数组放大10倍。例如,[0][0]中的1在新阵列中应为10x10平方的1。我提供了代码和输出,在填充较大的数组期间,这些代码和输出似乎工作正常,但随后会打印不同的值。为了限制测试过程中处理的变量数量,我目前只对行进行了实验。有人能想出这种情况发生的原因吗 public class Test { static byte[][] byteArray = {{1, 0}, {0, 1}}; public static void main(String

这个程序的目的是创建一个更大的字节数组,从原始数组放大10倍。例如,[0][0]中的1在新阵列中应为10x10平方的1。我提供了代码和输出,在填充较大的数组期间,这些代码和输出似乎工作正常,但随后会打印不同的值。为了限制测试过程中处理的变量数量,我目前只对行进行了实验。有人能想出这种情况发生的原因吗

public class Test 
{
static byte[][] byteArray =
{{1, 0},
 {0, 1}};

public static void main(String[] args)
{
    byte newarray[][] = converter();
    for(int i = 0; i < 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 i = 0; i < 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, 0},
{0, 1}};
公共静态void main(字符串[]args)
{
字节newarray[][]=转换器();
对于(int i=0;i<20;i++)
{
System.out.println(newarray[i][0]);
}
}
专用静态字节[][]转换器()
{
字节[][]b=新字节[20][20];
对于(int r=0;r<2;r++)
{
对于(int i=0;i<10;i++)
{
b[r+i][0]=byteArray[r][0];
System.out.println(byteArray[r][0]);
System.out.println(b[r+i][0]);
}
}
返回b;
}
}

一, 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

为什么不使用截断整数除法呢

static void printMat(byte[][] mat) 
// just a utility function to print a matrix
{ 
    for(byte[] row : mat)
    {
        System.out.println(Arrays.toString(row));
    }
}

private static byte[][] stretch(byte[][] bytes, int rfactor, int cfactor)
// stretch the matrix in 'bytes'
//stretch the rows by 'rfactor' and the columns by 'cfactor'
{ 
    // create an empty matrix:
    int rows = bytes.length*rfactor; // rows in the new matrix
    int cols = bytes[0].length*cfactor; // columns in the new matrix
    byte[][] out = new byte[rows][cols]; // our new, stretched matrix

    // loop through the rows and columns of the *new* matrix:
    for(int r = 0; r < rows; r++)
    {
        for(int c = 0; c < cols; c++)
        {
            // Divide the row and column indices by the 
            // appropriate factors to find the correct value
            // in the original matrix.
            // Integer division just drops any remainder,
            // which is what we want.
            out[r][c] = bytes[r/rfactor][c/cfactor];
        }
    }
    return out;
}

public static void main(String[] args) throws Exception 
{
    // your example:
    byte[][] byteArray =
        {{1, 0},
         {0, 1}};
    byte[][] newarray = stretch(byteArray, 10, 10);
    printMat(newarray);

    System.out.println();

    // can stretch any matrix by any dimensions:
    byte[][] byteArray2 =
        {{1, 2, 3},
         {4, 5, 6}};
    byte[][] newarray2 = stretch(byteArray2, 3, 2);
    printMat(newarray2);

}

我自己解决了这个问题。每次迭代都会损失九个位置。如果你按照代码做每一次计算,你就会意识到这一点。嘿,我真的不明白你在这里做什么。为什么要将bytes.length用于rfactor,而将bytes[0].length用于cfactor。另外,我通过在r中迭代正确的次数找到了答案,然后在其中嵌套了正确次数的c,我用字节[(r*10)+I][0]得到了正确的位置。我们函数之间的主要区别是,我在迭代新矩阵,而不是原始矩阵<代码>字节。长度是原始矩阵中的行数
bytes[0]
是第一行,因此
bytes[0]。length
是列数。(我假设输入是一个矩阵,所以所有行的长度都相同。)好的,我现在明白了。两种方法都有效。无论如何,谢谢你的帮助。
[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, 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, 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, 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, 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]
[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]
[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,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