powershell - 在powershell中有一种方法可以使用乘法将正数变为负数吗?
问题描述
我想知道是否有一种方法可以使用乘法将正数变为负数,就像$b = $a * -1
我正在寻找最成本合理的方式一样,因为我将在脚本中多次这样做。
-edit 此时我正在使用它,但在计算方面看起来非常昂贵:
$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]
顺便说一句:如果性能很重要,您可以通过使用范围表达式来创建要迭代的索引来加速循环,尽管以内存消耗为代价:
$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
推荐阅读
- angular - 在组件视图上刷新先前页面时,不会再次调用组件的 ngOnit()
- c# - 代码退出而不显示任何输出
- c - 使用flock() 锁定用fopen() 打开的文件
- html - 缩小窗口时如何使列保持静态?
- python - 如何让 Pygame 脚本在 macOS 上运行
- bash - 循环遍历 bash 中的数字文本文件并逐行添加数字
- javascript - 获取点击事件的 Typeahead.js 下拉项
- wordpress - 使用 ACF/post_meta 日期字段对帖子进行排序,如果未设置,则使用发布日期
- php - 我无法使用 isset($_POST['id'); 获取表单值
- python - 我如何撤消输入的任何单词的密码?