📄 sparsearraycreate.cpp
字号:
// sparseArrayCreate.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "matlab.hpp"
#include <stdlib.h>
#ifdef GCC
#ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0
#endif
#endif
int main(int argc, char* argv[])
{
/*创建稀疏矩阵*/
/*直接从原来矩阵的基础上生成稀疏矩阵*/
mwArray A,sparseA;
A = eye(5,5);
sparseA = sparse(A);
/*利用索引创建MATLAB矩阵*/
double inums[] = {3,4,5,4,5,6};
double jnums[] = {4,3,3,5,5,4};
mwArray indexS;
mwArray i(1,6,inums,NULL);
mwArray j(1,6,jnums,NULL);
indexS = sparse(i, j, 9, 8, 7);
/*从文本文件中读取数据并将其转换为稀疏矩阵*/
mwArray filename("sparsedata.dat");
mwArray data ;
mwArray spdata ;
mwArray fid;
fid = fopen(filename,"r");
data = fscanf(fid,"%f",mwArray(3*4));
data = reshape(data,3,4);
data = transpose(data);
cout << data << endl;
spdata = spconvert(data);
fclose(fid);
cout << "单位矩阵A:"<<A<<endl;
cout << "单位矩阵A的稀疏矩阵"<<sparseA<<endl;
cout << "利用索引创建的稀疏矩阵"<<indexS<<endl;
cout << "将上述稀疏矩阵转换为一般矩阵形式:"<<full(indexS)<<endl;
cout << "分析稀疏矩阵的属性:"<<endl;
cout << "\t 非零元素个数:"<<nnz(indexS)<<endl;
cout << "\t 最大可存储的非零元素个数:"<<nzmax(indexS)<<endl;
cout << "从文件读取的稀疏矩阵为 : " << spdata << endl;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -