⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 step.cpp

📁 使用矩阵类完成递推求解均值与方差,类的功能较强大,算法效率较高
💻 CPP
字号:
#include "matrix.h"  



////////////////////////////////
#ifndef _NO_NAMESPACE
using namespace std;
using namespace math;
#define STD std
#else
#define STD
#endif

#ifndef _NO_TEMPLATE
typedef matrix<double> Matrix;
#else
typedef matrix Matrix;
#endif

#ifndef _NO_EXCEPTION
#  define TRYBEGIN()	try {
#  define CATCHERROR()	} catch (const STD::exception& e) { \
						cerr << "Error: " << e.what() << endl; }
#else
#  define TRYBEGIN()
#  define CATCHERROR()
#endif

void WaitForEnterKey (int	print);

unsigned char dat[10][4][8][8][60]={
	//0--..\\样本1,1--..\\样本1,......,8--..\\样本9,9--..\\样本10
	//0--100,1--50,2--20,3--10
	//0--A向,1--B向,2--C向,3--D向,4--新A向,5--新B向,6--新C向,7--新D向
	//0--传感1,1--传感2,2--传感3,3--传感4,4--传感5,5--传感6,6--传感7,7--传感8
	//
	{
#include "..\\样本\\rmb00.txt"
	},
	{
#include "..\\样本\\rmb01.txt"
	},
	{
#include "..\\样本\\rmb02.txt"
	},
	{
#include "..\\样本\\rmb03.txt"
	},
	{
#include "..\\样本\\rmb04.txt"
	},
	{
#include "..\\样本\\rmb05.txt"
	},
	{
#include "..\\样本\\rmb06.txt"
	},
	{
#include "..\\样本\\rmb07.txt"
	},
	{
#include "..\\样本\\rmb08.txt"
	},
	{
#include "..\\样本\\rmb09.txt"
	}
};

const  int C= 4;		//类数
const  int V= 8;		//ROW维数
const  int NUM= 60;		//数据量
const  int DIR= 0;		//钱方向标号

Matrix	data(V,1),	check(V,1),	m(V,1),	mu(V,1),	E(V,V),Eu(V,V);
static	N	=	0;
	int	swatch=0,money=0,dir=0;

void	getdata()
{		   
	for (int	j=0;j<V;j++) 
	{
	   data(j,0)=(double)dat[swatch][money][dir][j][N];
	}
	N++;
	mu	=	m;
	Eu	=	E;
}
void	initialize()
{
	cout<<"请选择以第几组数据作为样本(1——10):\t";
	cin>>swatch;
	--swatch;
	cout<<"请选择采用哪一种面值作为样本(0——3)(0--100,1--50,2--20,3--10):\t";
	cin>>money;
	cout<<"请选择采用哪一种方向的数据(0——7)(0--A向,1--B向,2--C向,3--D向,4--新A向,5--新B向,6--新C向,7--新D向):\t";
	cin>>dir;
}
void	init()
{
	for (int	j=0;j<V;j++) 
	{
	   data(j,0)=(double)dat[swatch][money][dir][j][0];
	}
	m	=	data;
//	mu	=	m;
	N=1;
	E.Null();
	Eu	=	E;
	
}

void	countn()
{

	double	n=1/(double)(N+1),nn=(double)(N-1)/(double)(N);
	getdata();
	Matrix	det	=	(data-mu)*n;
	m	=	mu	+	det*n;
//	cout<<"countn	"<<N<<endl<<n<<endl;
	E	=	Eu*nn	+	((data	-	mu)*(~(data	-	mu)))*n;
}

	Matrix	u(V,1);
void	display()
{
	cout<<"	第"<<N-1<<"次取数据后的结果如下:"<<endl;
	cout<<"均矢:"<<endl;
	cout<<m;
	cout<<"协方差阵:"<<endl;
	cout<<E;
}

void	main()
{
	int	i,j,NUMM;
//	initialize();
	init();
	for(j=1;j<NUM;j++)
	{
		countn();
			display();

	}

  /*
			u.Null();
			NUMM	=	j+2;	
			for (i=0;i<NUMM;i++) 
			 {
			 for (j=0;j<V;j++) 
			   {
			   u(j,0)+=(double)dat[swatch][money][dir][j][i]/NUMM;
			   }
		   }
			cout<<u;
	*/
}

⌨️ 快捷键说明

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