Python中的二维数组分解

时间:2013-03-31 20:09:40

标签: python arrays decomposition

感谢您对Python 3的翻译的帮助,它将任意大小的输入数组分解为长度为4的较小的方形数组。

我已经尝试了块和numpy中的数组函数,但它们对此无用。

这是我在Perl中运行良好的代码,但我希望它与Python(效率)进行比较。

sub make_array {
my $input = shift;
my $result;
my @parts = split '-', $input;

$result = [];

# Test for valid number of lines in inputs
my $lines = scalar @parts;
if($lines % $width){
    die "Invalid line count $lines not divisible by $width" ;
    # Or could pad here by adding an entire row of '0'.
}

# Chunk input lines into NxN subarrays
# loop across all input lines in steps of N lines

my $line_width = 0;
for (my $nn=0;$nn<$lines;$nn+=$width){

    # make a temp array to handle $width rows of input
    my @temp = (0..$width-1);
    for my $ii (0..$width-1){

        my $p = $parts[$nn+$ii];
        my $padding_needed = length($p) % $width;
        if($padding_needed != 0) { 
            print "'$p' is not divisible by correct width of $width, Adding $padding_needed zeros\n";
            for my $pp (0..$padding_needed){ 
                $p .= "0";
            }
        }
        if($line_width == 0){ 
            $line_width = length($p);
        }
        $temp[$ii] = $p;
    }
    # now process temp array left to right, creating keys
    my $chunks = ($line_width/$width);
    if($DEBUG) { print "chunks: $chunks\n"; }
    for (my $zz =0;$zz<$chunks;$zz++){
        if($DEBUG) { print "zz:$zz\n"; }
        my $key;
        for (my $yy=0;$yy<$width;$yy++){
            my $qq = $temp[$yy];
            $key .= substr($qq,$zz*$width, $width) . "-";
        }
        chop $key; # lose the trailing '-'
        if($DEBUG) { print "Key: $key\n"; }
        push @$result, $key;
    }
}        

if($DEBUG){
    print "Reformatted input:";
    print Dumper $result;
    my $count = scalar @$result;
    print "There are $count keys to check against the lookup table\n";
}
return $result;
}

例如,我有以下12 x 12矩阵:

000011110011
000011110011
000011110011
000011110011
000011110011
000011110011
000011110011
000011110011

我希望它分解为长度为4的6个方形子矩阵:

0000 1111 0011
0000 1111 0011
0000 1111 0011
0000 1111 0011

0000 1111 0011
0000 1111 0011
0000 1111 0011
0000 1111 0011

原始矩阵来自文件(程序应该从文本文件中读取),格式如下:

000011110011,000011110011,000011110011,000011110011,000011110011,000011110011,000011110011,000011110011

因此,程序需要用连字符拆分它,并将每个块作为大矩阵的一行。 6个子矩阵应采用相同的输入格式,因此第一个将是:

0000,0000,0000,0000

程序应该将任何输入矩阵分解为长度为j的正方形矩阵,比如4,如果原始矩阵的大小不是4的倍数,那么它应该忽略不能形成4x4矩阵的剩余块。

原始输入文件中可能有几个不同大小的大型矩阵,断行为分隔符。例如,原始大矩阵和其他rmatrix在文本文件中看起来如下所示:

000011110011,000011110011,000011110011,000011110011,000011110011,000011110011,000011110011,000011110011\n
0101,0101,0101,0101

并检索2组子阵列,6个4x4阵列中的一个和第二个4x4阵列中的一个。如果你解决单一案件当然没问题。

1 个答案:

答案 0 :(得分:1)

numpy这很容易。假设我们有一个12x12阵列;

In [1]: import numpy as np

In [2]: a = np.arange(144).reshape([-1,12])

In [3]: a
Out[3]: 
array([[  0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11],
       [ 12,  13,  14,  15,  16,  17,  18,  19,  20,  21,  22,  23],
       [ 24,  25,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35],
       [ 36,  37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47],
       [ 48,  49,  50,  51,  52,  53,  54,  55,  56,  57,  58,  59],
       [ 60,  61,  62,  63,  64,  65,  66,  67,  68,  69,  70,  71],
       [ 72,  73,  74,  75,  76,  77,  78,  79,  80,  81,  82,  83],
       [ 84,  85,  86,  87,  88,  89,  90,  91,  92,  93,  94,  95],
       [ 96,  97,  98,  99, 100, 101, 102, 103, 104, 105, 106, 107],
       [108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119],
       [120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131],
       [132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143]])

要选择左上角的4x4阵列,请使用slicing

In [4]: a[0:4,0:4]
Out[4]: 
array([[ 0,  1,  2,  3],
       [12, 13, 14, 15],
       [24, 25, 26, 27],
       [36, 37, 38, 39]])

右下角子阵列是:

In [7]: a[8:12,8:12]
Out[7]: 
array([[104, 105, 106, 107],
       [116, 117, 118, 119],
       [128, 129, 130, 131],
       [140, 141, 142, 143]])

你可以猜到其余的......