estimate.cpp

来自「Parzen 窗 和 K近邻法进行概率密度估计 还带一个示波器控件.」· C++ 代码 · 共 68 行

CPP
68
字号
// Estimate.cpp: implementation of the CEstimate class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#include "Estimate.h"

#include <cmath>
#include <assert.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CEstimate::CEstimate(int nDim)
{
	assert(nDim > 0);
	m_nDim = nDim;
}

CEstimate::~CEstimate()
{

}

bool	CEstimate::AddSampleData(vector<double> x)
{
	if (x.size() != m_nDim)
		return false;

	m_listSample.push_back(x);
	return true;
}

bool CEstimate::ClearSampleData()
{
	m_listSample.resize(0);
	return true;
}

//计算两个矢量之间的距离 , 2范数
double CEstimate::Distance(vector<double> x1, vector<double> x2)
{

	assert(x1.size() == x2.size());

	vector<double>::iterator vi1,vi2;
	
	double temp = 0.0;

	for (vi1 = x1.begin(), vi2 = x2.begin(); 
			(vi1 != x1.end()) && (vi2 != x2.end());
				vi1++,vi2++)
				{
					temp += (*vi1 - *vi2)*(*vi1 - *vi2);
				}
	

	return sqrt(temp);

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?