【问题标题】:Scaling An Array (Matrix)缩放数组(矩阵)
【发布时间】:2013-05-09 12:14:41
【问题描述】:

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

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

【问题讨论】:

  • 我自己解决了这个问题。每次迭代都失去了九个位置。如果你按照代码做每一个计算,你就会意识到这一点。

标签: java arrays matrix byte scaling


【解决方案1】:

为什么不直接使用截断整数除法来获得优势:

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);

}

输出:

[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, 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]

[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]

【讨论】:

  • 嘿,我不太明白你在这里做什么。为什么将 bytes.length 用于 rfactor,将 bytes[0].length 用于 cfactor。另外,我通过迭代 r 正确的次数来找出答案,然后将 c 嵌套在正确的次数内,然后我做了 bytes[(r*10)+i][0] 以产生正确的位置。
  • 我们函数之间的主要区别在于我迭代的是新矩阵而不是原始矩阵。 bytes.length 是原始矩阵中的行数。 bytes[0] 是第一行,所以 bytes[0].length 是列数。 (我假设输入是一个矩阵,所以所有的行都是相同的长度。)
  • 好的,我现在明白了。无论哪种方式都有效。无论如何,感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2021-02-18
  • 1970-01-01
  • 2015-02-13
  • 2016-04-15
  • 2013-11-19
  • 1970-01-01
  • 1970-01-01
  • 2015-04-20
相关资源
最近更新 更多