在SWIFT中,有一种快速有效的方法可以使数组的所有值都改变符号(正值变为负值,反之亦然)?
例如:
let x: [Double] = [1.0, 2.0, -3.0, 4.0, -5.0]
Run Code Online (Sandbox Code Playgroud)
应该成为:
// x = [-1.0, -2.0, 3.0, -4.0, 5.0]
Run Code Online (Sandbox Code Playgroud) 我写了一个算法是 Swift,用于在 Swift 数组中查找最大值及其索引。这是受到 Matlab & Octave 中的“max.m”函数的启发。
这里的专家能否提出一种在速度方面改进该算法的方法?我的意思是它可以做得更快,或者您认为这是大型阵列(有时是 15000 个样本)的合理方法。
public func max (y: [Double]) -> (Int, Double) {
let inLen = y.count
var out = Double()
var outp = Int()
if (1 == inLen) { // if only one element
out = y[0]
outp = 0
} else if (0 == inLen) { // if no elements
out = -1
outp = -1
} else {
out = y[0]
outp = 0
for ii in 1...inLen-1 {
if …Run Code Online (Sandbox Code Playgroud) 我正在查看Accelerate以在Swift中计算数组的均值和标准差。
我可以这样说。如何做标准偏差?
let rr: [Double] = [ 18.0, 21.0, 41.0, 42.0, 48.0, 50.0, 55.0, 90.0 ]
var mn: Double = 0.0
vDSP_meanvD(rr, 1, &mn, vDSP_Length(rr.count))
print(mn) // prints correct mean as 45.6250
// Standard Deviation should be 22.3155
Run Code Online (Sandbox Code Playgroud)