简体   繁体   English

Perl PDL 未按预期工作

[英]Perl PDL not working as expected

I really don't understand PDL's input functions. PDL的输入函数我是真的不懂。 Personally, I've been using the rcols feature to create pdls, as was recommended to me in various places around the web.就个人而言,我一直在使用 rcols 功能来创建 pdls,正如在 web 周围的各个地方向我推荐的那样。

I have an input file like this:我有一个这样的输入文件:

 3 -4 -1
 0 5 2
 3 5 6
 2 5 2
 5 5 5
 5 5 6

which, I want to assign to a Piddle.其中,我想分配给一个 Piddle。 When I assign it to a piddle like so,当我像这样将它分配给一个 piddle 时,

my @pdls = rcols $in_fh, { COLSEP => "\\s" } ;
my $pdl = pdl(@pdls[1 .. $#pdls]);

When I print @pdls this is printed:当我打印 @pdls 时,打印出来的是:

[
 [ 3  0  3  2  5  5]
 [-4  5  5  5  5  5]
 [-1  2  6  2  5  6]
]

Which made me think it pulled my file by columns, and not rows.这让我觉得它按列而不是按行提取了我的文件。 Which makes sense looking at the code, really.看代码是有道理的,真的。 When I saved this output to a file(After stripping out all the brackets) this is how it looked.当我将这个 output 保存到一个文件中时(去掉所有括号后),它看起来是这样的。 : :

3  0  3  2  5  5
-4  5  5  5  5  5
-1  2  6  2  5  6

When I ran the same script on the new input file, the result does not follow the same process as before:当我在新的输入文件上运行相同的脚本时,结果并不遵循与以前相同的过程:

[
 [ 0 -4 -1]
 [ 3  0  0]
 [ 0  5  2]
 [ 0  0  0]
 [ 0  5  6]
 [ 3  0  0]
 [ 0  5  2]
 [ 2  0  0]
 [ 0  5  5]
 [ 5  0  0]
 [ 0  5  6]
 [ 5  0  0]
]

And I have no idea why it is doing so.我不知道为什么要这样做。 In essence, I want to be able to read my text file into a piddle.本质上,我希望能够将我的文本文件读入 piddle。 Does anyone see what I'm missing, or able to offer any explanation?有没有人看到我遗漏了什么,或者能够提供任何解释?

Thanks for any help.谢谢你的帮助。

As is occasionally the case in PDL, the design of things with several dimensions can be a bit counter-intuitive.正如 PDL 中偶尔出现的情况,具有多个维度的事物的设计可能有点违反直觉。 But it is designed overall so it's easy to just adjust dimensions.但它是整体设计的,因此很容易调整尺寸。 Here, rcols and wcols treat data in files in a FORTRAN-style column-major way.在这里, rcolswcols以 FORTRAN 风格的列优先方式处理文件中的数据。 It is easy to adjust that using the transpose method:使用transpose方法很容易调整:

pdl> p $x = sequence(3,4)

[
 [ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]
]

pdl> wcols($x->transpose, 'myfile')

pdl> p pdl(rcols('myfile', {colsep => qr/\s+/}))->transpose
Reading data into ndarrays of type: [ Double Double Double ]
Read in  4  elements.

[
 [ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]
]

Maybe its just better to make a "3,6 matrix of zeros" then set in each value individually, (which means putting the data from a file into a 1D pdl() first) I would use a open() to read it into a scaler then put that in a 1D piddle; 也许最好先制作一个“ 3,6的零矩阵”,然后分别在每个值中设置(这意味着首先将文件中的数据放入一维pdl()中),我将使用open()将其读入然后,将一个缩放器放入一维陷阱中; which can be rather involved ... once you get it in a 1D piddle do this: 可能涉及其中……一旦您将其放入一维小提琴中,请执行以下操作:

open(FILE,"yourfile"); while (<FILE>) { $x = $_; } 
    close FILE;

   $y = zeros(3,6);
p  $x = sequence(18);
 [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17]
for $c(0..5) { for $d(0..2) {  $y($d,$c) .= $x($e++)   }}  
p $y
 [ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]
 [12 13 14]
 [15 16 17]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM