using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;
namespace Mahalanobis_Distance { public partial class Form1 : Form { private int[,] nums; private int _count = 0; int x, y;
public Form1() { InitializeComponent(); }
private void listBox2_KeyPress(object sender, KeyPressEventArgs e) { if (!Char.IsDigit(e.KeyChar) && e.KeyChar != Char.Parse(",")) e.Handled = true; }
// Compute and display result private void btnCompute_Click(object sender, EventArgs e) { meanX1.Text = AverageX(nums).ToString(); variance.Text = varianceX(nums).ToString(); // stdDev.Text = stdDeviationOfX; } // Calculate standard Deviation Value private double stdDeviation (double varianceX) { double stdDevX = Math.Sqrt(varianceX); return stdDevX;
}
// Calculate Variance Value private double varianceX(int[,] nums)
{ int NumMatrix =0; double varX =0; double sumSquare=0;
// Get the Means@Average of X values double avgX = AverageX(nums); // Declare the total Matrix NumMatrix = nums.Length; // Calculate the variance for (int i = 0; i <= NumMatrix; i++) { sumSquare += Math.Pow((double)nums[i, 0]- avgX, 2); } varX= sumSquare/NumMatrix; return varX; }
// Average value of X matrik private double AverageX(int[,] nums) {
double avgValueX = 0; double sumValueX = 0;
for (int i = 0; i <=nums.GetUpperBound(0); i++) { // Total Value Matrix X sumValueX += (double)nums[i, 0]; } avgValueX = sumValueX / (nums.GetUpperBound(0) + 1); return avgValueX;
} // insert input matrix example (7,8) in listBox and stored in 2-dimensional array
private void btnAdd_Click(object sender, EventArgs e) { nums = new int[listBox2.Items.Count, 2]; for (int i = 0; i < this.listBox2.Items.Count; i++) { if (_count < 20 && this.listBox2.Items[i].ToString() != "" && this.listBox2.Items[i].ToString().Contains(",")) { string t = this.listBox2.Items[i].ToString();
string sx = t.Substring(0, t.IndexOf(",")); Int32.TryParse(sx, out x);
t = t.Substring(t.IndexOf(",") + 1); Int32.TryParse(t, out y);
this.nums[_count, 0] = x; this.nums[_count, 1] = y;
_count++; } } }
// Show input matrix in ListBox2 private void showMatrixList(int[,] nums) { for (int i = 0; i < listBox2.Items.Count; i++) { this.listBox1.Items.Add("X:" + x.ToString() + "; Y:" + y.ToString()); } }
} }
|