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 });
}
}