dozug64282 2018-07-17 11:44
浏览 809

使用自定义Golang库计算算术平均值

package main

import (
    "fmt"

    maths "github.com/ematvey/go-fn/fn"
)

var avg float64

func main() {

    A := []float64{2, 3, 5, 7, 11, 13}

    avg := maths.ArithMean(A)
    fmt.Println(avg)
}

I am not able to call the ArithMean function. It is giving the error:

cannot use A (type []float64) as type *fn.Vector in argument to fn.ArithMean

  • 写回答

1条回答 默认 最新

  • dongzao4503 2018-07-17 12:15
    关注

    package fn

    type Vector
    
    type Vector struct {
        A   []float64 // data
        L   int       // length
    }
    

    For example,

    package main
    
    import (
        "fmt"
    
        maths "github.com/ematvey/go-fn/fn"
    )
    
    func main() {
        a := []float64{2, 3, 5, 7, 11, 13}
        v := &maths.Vector{A: a, L: len(a)}
        avg := maths.ArithMean(v)
        fmt.Println(avg)
    }
    

    Output:

    6.833333333333333
    

    Other statistical packages may be easier and more intuitive to use.

    For example,

    func Mean

    func Mean(x, weights []float64) float64
    

    Mean computes the weighted mean of the data set.

    sum_i {w_i * x_i} / sum_i {w_i}
    

    If weights is nil then all of the weights are 1. If weights is not nil, then len(x) must equal len(weights).

    package main
    
    import (
        "fmt"
    
        "gonum.org/v1/gonum/stat"
    )
    
    func main() {
        a := []float64{2, 3, 5, 7, 11, 13}
        mean := stat.Mean(a, nil)
        fmt.Println(mean)
    }
    

    Output:

    6.833333333333333
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64