c++ - 我有一个矩阵 mtx 文件直到整理:
问题描述
我有两种类型的代码:
这个,我可以从mtx文件中提取所有字符,但是运行的时候,第一行的最后两个字取不出来,不能跳过有'%'字符的行和空行,和我无法将矩阵维度及其值分离到它们自己的一组数组中。此外,带有数字的第一行是整数,用于计算维度和条目的数量,我想创建一个 for 循环来计算主条目并将值分隔在其自己的数组中,以便我可以用科学计数法计算它们的 frobinius_norm。
#include <iostream>
#include <cmath>
#include <vector>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;
void processLine(const string &line)
{
string first, second, third;
if ( line[0]&&line[1] == '%' )
{
//extract last 2 words
cout <<line << endl;
} else if (line[0]=='%')
{
//ignore the lines
cout << line << endl;
} else {
/*seperate matrix dimensions and values,
but the first one is x*y dimension and number of entries*/
cout << line << endl;
}
}
void processFile(const char *filename)
{
ifstream inf(filename);
if ( inf.is_open() ) {
string line,type;
while ( getline(inf,line) ) {
processLine(line);
}
} else {
cout << "Sorry, but the file don't exist!\n";
}
}
int main(int argc, char *argv[])
{
if (argc==2){
cout <<"Filename: " << argv[1] << endl;
processFile(argv[1]);
} else {
cout << "give a filename\n";
}
}
而这个,我可以成功提取第一个矩阵文件的所有信息:sparse01.mtx:
%%MatrixMarket矩阵坐标模式通用9 9 50 1 1 2 1 4 1 5 1 6 1 7 1 8 1 9 1 2 2 3 2 ... 9 9
但是 sparse02.mtx 和 sparse03.mtx 矩阵我无法修改代码以给出正确的结果,因为我不能忽略带有开头字符 '%' 和空行的行。我无法真正计算主菜并分开尺寸和值,因此我无法计算 frobinius 范数......
int main(int argc, char *argv[])
{
string filename, line, type1, type2, header;
int x, y, entry, count=0;
double element;
char norm;
float frobenius_norm;
ifstream openfile;
filename = argv[1];
openfile.open(argv[1]);
if(!openfile.is_open())
{
cout << "Sorry, but the file don't exist!\n";
exit(1);
}
for(int i =0;i<2;i++)
{
getline(openfile,line,' ');
}
getline(openfile,header,' ');
getline(openfile,type1,' ');
getline(openfile,type2);
if(getline(openfile,line).peek()=='%'){
cout << "lol"<<endl;
}
openfile >> x >> y >> entry;
cout <<"Filename: " << filename << endl;
cout <<"Dimensions: " << x << " x " << y << endl;
cout <<"Entries: " << entry <<endl;
if(header == "coordinate")
{
cout <<"Sparse: "<< "true" <<endl;
}
else if ((x*y/2)<entry||header == "array")
{
cout <<"Sparse: "<< "false" <<endl;
}
cout <<"Type: "<< type1 << ' ' << type2 <<endl;
if(type1 == "pattern"){
norm = '-';
cout <<"Frobenius norm: "<< norm <<endl;
} else {
frobenius_norm = 100;
cout <<"Frobenius norm: "<< frobenius_norm <<endl;
}
return 0;
}
输出应该是这样的
文件名:sparse01.mtx 尺寸:9 x 9 条目:50 稀疏:true 类型:模式一般 Frobenius 范数:-
%%MatrixMarket 矩阵坐标模式一般 9 9 50 1 1 2 1 4 1 5 1 6 1 7 1 ...(截断)9 9
sparse03 矩阵的输出是这样的: 文件名:sparse03.mtx 尺寸:5 x 5 条目:10 稀疏:true 类型:真正的一般 Frobenius 范数:1.2e+02
这是 sparse03.mtx 文件(这不是代码,而是输入文件):
%%MatrixMarket matrix coordinate real general
%
% Hello
% How
% Are
% you
% doing
%
5 5 10
1 1 11.0
1 5 15.0
2 3 23.0
2 4 24.0
3 2 32.0
3 5 35.0
4 1 41.0
5 2 52.0
5 4 54.0
5 5 55.0
我还希望我的代码能够计算密集和特殊的矩阵:
请记住,我使用 argv[1] 作为输入参考
解决方案
推荐阅读
- python - 在 Python 中使用 MultiIndex 和 to_excel 时创建的标题下方的空行
- angular - 如何将完整 Angular 应用程序的工作源代码发布到 npm?
- angularjs - 为什么 $compile 在 ng-repeat 中创建多个元素?
- .net - 作为使用免费版 Visual Studio 的 Windows Installer 设置的一部分,如何更改 VB .net 应用程序的设置?
- asp.net-mvc - MVC:提交 Ajax.Beginform 后清除模型值
- python - 在最新版本的 python pandas 中创建 pandas.Series 在 Matlab 中不起作用
- sql - 根据单个单元格中的多个条件选择记录
- android - app:layout_constraint 值在 xml 源视图中始终显示为未定义 - Android Studio 3.2
- django - 在 django-mptt 树结构中删除子节点的正确方法是什么
- grails - grails3静态图片请求获取404