簡體   English   中英

Perl PDL 中 R 的 ifelse 的等價物是什么

[英]What's the equivalent of R's ifelse in Perl PDL

我是 PDL 的新手。 R 的 ifelse() 方法可以進行條件元素選擇。 例如,

x <- c(1,2,3,4)
ifelse(x%%2, x, x*2)
# [1] 1 4 3 8

任何人都知道如何在 PDL 中執行此操作? 我知道你可以像下面那樣做,但有沒有更好的方法?

pdl(map { $_ % 2 ? $_ : $_*2 } @{$x->unpdl} )
#! /usr/bin/perl
use warnings;
use strict;

use PDL;

my $x     = 'PDL'->new([1, 2, 3, 4]);
my $where = ! ($x % 2);               # [0 1 0 1]
my $y     = $x * ($where + 1);
print $y;                             # [1 4 3 8]

或者,不久

my $y = $x * ( 2 - $x % 2 );

我自己回答問題。 可能是這樣的,

use PDL;                                                                      

sub ifelse {                                                                  
    my ( $test, $yes, $no ) = @_;                                             

    $test = pdl($test);                                                       
    my ( $ok, $nok ) = which_both($test);                                     

    my $rslt = zeros( $test->dim(0) );                                        

    unless ( $ok->isempty ) {                                                 
        $yes = pdl($yes);                                                     
        $rslt->slice($ok) .= $yes->index( $ok % $yes->dim(0) );               
    }                                                                         
    unless ( $nok->isempty ) {                                                
        $no = pdl($no);                                                       
        $rslt->slice($nok) .= $no->index( $nok % $no->dim(0) );               
    }                                                                         
    return $rslt;                                                             
}                                                                             

my $x = pdl( 1, 2, 3, 4 );                                                    
say ifelse( $x % 2, $x, $x * 2 );       # [1 4 3 8]                                             
say ifelse( $x % 2, 5, sequence( 3 ) ); # [5 1 5 0]                                      
say ifelse( 42, $x, $x * 2 );           # [1]

在 PDL 中,這種事情的一般解決方案可能涉及切片和類似的東西。 查看 PDL (2.077) 的最新發行說明,其中有一個新的where_both ,我記得這個問題(披露:我是當前的維護者)。 雖然您的具體問題只涉及更改偶數的值,但我還將展示將賠率加 2 的情況:

my ($odd, $even) = where_both($x, $x % 2);
$odd += 2, $even *= 2; # the "," form is just a flourish, it could be 2 lines

它以適當的 PDL 風格是高效的,因為$x的掃描只發生一次(你不會驚訝地發現它也在幕后使用which_both ),並且突變只查看相關位的切片。 這與您的代碼非常相似,但它被捕獲到一個可廣泛重復使用的小型 function 中。(如果您有興趣,我寫它是為了將 TriD EuclidAxes 的東西從使用 Perl for 循環轉換為實際使用 ndarrays)

$x ? $y : $z $x ? $y : $z 我不認為這是樣式和口味的問題

sub ifelse {
    my ($x,$y,$z) = @_;

    $x ? $y : $z ;
    if($x){$y}else{$z} ;
    [$y,$z]->[!$x] ;
    [$z,$y]->[!!$x] ;
    ($x && $y) || $z ;        # valid only if $y is always true
    (!$x && $z) || $y ;       # valid only if $z is always true
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM