缩放数组(矩阵) [英] Scaling An Array (Matrix)

查看:158
本文介绍了缩放数组(矩阵)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The intention of this program is to create a larger array of bytes scaled up by a factor of 10 from the original array. For example, the 1 in [0][0] should be a 10x10 square of 1's in the new array. I provide the code and the output, which seems to work properly during population of the larger array, but then prints different values. I'm currently experimenting with just the rows in order to limit the number of variables I'm dealing with during testing. Can anyone think of a reason why this happens?

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

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 just use truncated integer division to your advantage:

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]

这篇关于缩放数组(矩阵)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆