gpt4 book ai didi

powershell - 在 powershell 中有一种方法可以不用乘法将正数变成负数吗?

转载 作者:行者123 更新时间:2023-12-02 23:37:08 24 4
gpt4 key购买 nike

我想知道是否有一种方法可以不用像 $b = $a * -1
这样的乘法将正数变成负数我正在寻找成本最合理的方法,因为我要在脚本中多次执行此操作。

-编辑在这一点上,我正在使用它,但在计算方面看起来非常昂贵:

    $temp_array = New-Object 'object[,]' $this.row,$this.col

for ($i=0;$i -le $this.row -1 ; $i++) {
for ($j=0;$j -le $this.col -1 ; $j++) {
$digit = $this.data[$i,$j] * -1
$temp_array[$i,$j] = 1 / ( 1 + [math]::exp( $digit ) )
#[math]::Round( $digit ,3)
}
}
$this.data = $temp_array

最佳答案

要无条件地将一个正数转换为它的负数(或者,更一般地说,翻转一个数字的符号),只需使用一元-运算符:

 PS> $v = 10; -$v
-10

适用于您的情况:

 $digit = -$this.data[$i,$j]

顺便说一句:如果性能很重要,您可以通过使用.. 来加速循环,range operator 创建要迭代的索引:

$temp_array = New-Object 'object[,]' $this.row,$this.col

for ($i in 0..($this.row-1)) {
for ($j in 0..($this.col-1)) {
$digit = - $this.data[$i,$j]
$temp_array[$i,$j] = 1 / ( 1 + [math]::exp( $digit ) )
}
}
$this.data = $temp_array

关于powershell - 在 powershell 中有一种方法可以不用乘法将正数变成负数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52634418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com