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

📄 test.cpp

📁 计算一个以文本格式存储的矩阵的相关系数矩阵
💻 CPP
字号:
#include <stdio.h>
#include <stdlib.h>
#include "tvec.h"
#include "tmat.h"
#include <crtdbg.h>
typedef tmat<double> CMatrix;
typedef tvec<double> CVector;


/****************************************************************************************
得到一个矩阵的相关系数矩阵
读取一个文本格式的矩阵文件-格式见例子数据
*****************************************************************************************/                  
CVector V[3];

int main()
{


	ifstream inPFile("data.txt",ios::in);
	if(!inPFile){
		cerr<<"File could not be opened\n";
		exit(1);
	}
	long nRow=0;
	long nCol=0;
	

	double l=0;
	//读取行列值
	for(long nRecord=0;nRecord<2;nRecord++)
	{
		inPFile>>l;
		if(nRecord==0) nRow=l;
		if(nRecord==1) nCol=l;
	}

	long k=0;
	long nData=nRow*nCol;
	double *tempV=new double[nData];
	while(inPFile>>l)
	{
		
		tempV[k]=l;	
		k++;
	}
	
	CMatrix mTQ(nRow,nCol);
	long i;
	for(i=0;i<nData;i++)
	{
		mTQ(i+1)=tempV[i];
	}

	//进行标准化
    CMatrix standQ;
	standQ=standarized(mTQ);

	CMatrix D;
	D=transpose(standQ);
 
	CMatrix E;
	E=standQ*D;
	
	FILE *ffp;
	if ((ffp=fopen("Relation.txt","w+"))==NULL) {
		printf("Cannot open file strike any key exit!");
		exit(1);
	}
    CMatrix mRelation(nRow,nCol);//相关系数矩阵
	for (long o1=1;o1<=nRow;o1++) {
		for (long o2=1;o2<=nRow;o2++) {
			double dttt=E(o1,o2)/nCol;
			mRelation(o1,o2)=dttt;
			fprintf(ffp,"%.4f	",dttt);
		}

		fprintf(ffp,"\n");

	}
	fclose(ffp);

	delete tempV;
 
	return 0;
}

⌨️ 快捷键说明

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