高斯函数

在这里插入图片描述
在这里插入图片描述

0.00291504   0.01306431   0.02153941   0.01306431   0.00291504   
0.01306431   0.05855019   0.09653295   0.05855019   0.01306431   
0.02153941   0.09653295   0.15915590   0.09653295   0.02153941   
0.01306431   0.05855019   0.09653295   0.05855019   0.01306431   
0.00291504   0.01306431   0.02153941   0.01306431   0.00291504   

-2  0.05349813
-1  0.2397619
0  0.3953007
1  0.2397619
2  0.05349813

代码:

using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class Gauss : MonoBehaviour
{
    void Start()
    {
        float pi = 2 * Mathf.PI; 
        float k = 1.0f / pi; //这里方差取1
        float e = 2.718281828459f;
        float total = 0;
        for (int i = -4; i <= 4; ++i)
        {
            for (int j = -4; j <= 4; ++j)
            {
                float n = -0.5f * (i * i + j * j);
                float result = k * Mathf.Pow(e, n);
                total += result;
            }
        }

        string str = string.Empty;
        Dictionary<int, float> dict = new Dictionary<int, float>();

        int M = 2;

        for (int i = M; i >= -M; --i)
        {
            for (int j = -M; j <= M; ++j)
            {
                float n = -0.5f * (i * i + j * j);
                float result = k * Mathf.Pow(e, n);
                result /= total;
                string temp = result.ToString("0.00000000");
                str += temp;
                str += "   ";
                if (!dict.ContainsKey(j)) dict[j] = result;
                else dict[j] += result;
            }
            str += "\r\n";
        }

        str += "\r\n";
        foreach(var item in dict)
        {
            str += item.Key + "  " + item.Value;
            str += "\r\n";
        }

        Debug.LogError("dir =" + Application.persistentDataPath);
        string filePath = Application.persistentDataPath + "/a.txt";
        if (!File.Exists(filePath))
        {
            FileStream stream = File.Create(filePath);
            stream.Close();
        }
        File.WriteAllLines(filePath, new string[] { str });
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值