访问PDL对象中数据的最佳实践
例如,我有一个2维的PDL:
my $data = random(4,4);
我想计算PDL上下文之外的0,0和1,0元素的总和。 (例如$ data-> slice('0,0') + $ data-> slice('1,0)
,它仍然返回PDL对象) 正确的方法是什么?
I have a 2 dimensional pdl,for example:
my $data = random(4,4);
and I want to compute the sum of the 0,0 and the 1,0 element outside of the pdl context.
(Not like, for example $data->slice('0,0') + $data->slice('1,0)
, which still returns a pdl object)
What is the correct way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想将单个元素作为普通perl标量提取,请在上使用 slice :
将整个ndarray对象转换为嵌套的perl数组,请使用
umpdl
请注意,perl数组中元素的索引是在ndarray对象中完成的横向。例如,
If you're looking to extract individual elements as normal Perl scalars, use
at
instead ofslice
:To convert the entire ndarray object into nested Perl arrays, use
unpdl
Note that the indexing of elements in the Perl arrays is the transverse of that done in the ndarray objects. For example,
Diab的答案非常适合所问的问题。但是,由于问题包括询问“最佳实践”,我建议通常在使用PDL时,应该考虑以阵列编程样式立即考虑所有操作,并且/或考虑您的代码在给出更高的情况下如何运行 - 维度ndarrays(即它将如何“广播”)。
一种使给定代码广播的方法,以便将4x4的1个元素ndarray归还,但是如果给出了4x4x3 ndarray:3元素:
Diab's answer is perfect for the question as asked. However, since the question includes asking about "best practice", I would recommend that usually when using PDL, one should consider operating on everything at once, in array-programming style, and/or to consider how your code would operate when given higher-dimensional ndarrays (i.e. how it would "broadcast").
A way to make the given code broadcast so that it would give back a 1-element ndarray for a 4x4, but a 3-element if given a 4x4x3 ndarray: