从文件加载矩阵
我需要读取包含矩阵数据的文件并在程序中创建矩阵。矩阵文件格式看起来与此类似:
# Matrix made by matblas from blosum62.iij
# * column uses minimum score
# BLOSUM Clustered Scoring Matrix in 1/2 Bit Units
# Blocks Database = /data/blocks_5.0/blocks.dat
# Cluster Percentage: >= 62
# Entropy = 0.6979, Expected = -0.5209
A R N D C Q E G H I L K M F P S T W Y V B Z X *
A 4 -1 -2 -2 0 -1 -1 0 -2 -1 -1 -1 -1 -2 -1 1 0 -3 -2 0 -2 -1 0 -4
R -1 5 0 -2 -3 1 0 -2 0 -3 -2 2 -1 -3 -2 -1 -1 -3 -2 -3 -1 0 -1 -4
N -2 0 6 1 -3 0 0 0 1 -3 -3 0 -2 -3 -2 1 0 -4 -2 -3 3 0 -1 -4
D -2 -2 1 6 -3 0 2 -1 -1 -3 -4 -1 -3 -3 -1 0 -1 -4 -3 -3 4 1 -1 -4
C 0 -3 -3 -3 9 -3 -4 -3 -3 -1 -1 -3 -1 -2 -3 -1 -1 -2 -2 -1 -3 -3 -2 -4
Q -1 1 0 0 -3 5 2 -2 0 -3 -2 1 0 -3 -1 0 -1 -2 -1 -2 0 3 -1 -4
E -1 0 0 2 -4 2 5 -2 0 -3 -3 1 -2 -3 -1 0 -1 -3 -2 -2 1 4 -1 -4
G 0 -2 0 -1 -3 -2 -2 6 -2 -4 -4 -2 -3 -3 -2 0 -2 -2 -3 -3 -1 -2 -1 -4
H -2 0 1 -1 -3 0 0 -2 8 -3 -3 -1 -2 -1 -2 -1 -2 -2 2 -3 0 0 -1 -4
I -1 -3 -3 -3 -1 -3 -3 -4 -3 4 2 -3 1 0 -3 -2 -1 -3 -1 3 -3 -3 -1 -4
L -1 -2 -3 -4 -1 -2 -3 -4 -3 2 4 -2 2 0 -3 -2 -1 -2 -1 1 -4 -3 -1 -4
K -1 2 0 -1 -3 1 1 -2 -1 -3 -2 5 -1 -3 -1 0 -1 -3 -2 -2 0 1 -1 -4
M -1 -1 -2 -3 -1 0 -2 -3 -2 1 2 -1 5 0 -2 -1 -1 -1 -1 1 -3 -1 -1 -4
F -2 -3 -3 -3 -2 -3 -3 -3 -1 0 0 -3 0 6 -4 -2 -2 1 3 -1 -3 -3 -1 -4
P -1 -2 -2 -1 -3 -1 -1 -2 -2 -3 -3 -1 -2 -4 7 -1 -1 -4 -3 -2 -2 -1 -2 -4
S 1 -1 1 0 -1 0 0 0 -1 -2 -2 0 -1 -2 -1 4 1 -3 -2 -2 0 0 0 -4
T 0 -1 0 -1 -1 -1 -1 -2 -2 -1 -1 -1 -1 -2 -1 1 5 -2 -2 0 -1 -1 0 -4
W -3 -3 -4 -4 -2 -2 -3 -2 -2 -3 -2 -3 -1 1 -4 -3 -2 11 2 -3 -4 -3 -2 -4
Y -2 -2 -2 -3 -2 -1 -2 -3 2 -1 -1 -2 -1 3 -3 -2 -2 2 7 -1 -3 -2 -1 -4
V 0 -3 -3 -3 -1 -2 -2 -3 -3 3 1 -2 1 -1 -2 -2 0 -3 -1 4 -3 -2 -1 -4
B -2 -1 3 4 -3 0 1 -1 0 -3 -4 0 -3 -3 -2 0 -1 -4 -3 -3 4 1 -1 -4
Z -1 0 0 1 -3 3 4 -2 0 -3 -3 1 -1 -3 -1 0 -1 -3 -2 -2 1 4 -1 -4
X 0 -1 -1 -1 -2 -1 -1 -1 -1 -1 -1 -1 -1 -1 -2 0 0 -2 -1 -1 -1 -1 -1 -4
* -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 1
字母具有恒定的位置,因此由于我只对其中四个感兴趣,所以我可以在程序中分配 x
和 y
。我需要的只是一个包含值的矩阵,我可以通过给出函数“GetValue”“x”和“y”来搜索值。
这是我的代码。它只是头文件中定义的类的一部分,其中包含并通过矩阵搜索值。也许这不是优雅的方式,但我没有那么多时间,所以现在我想快速完成。稍后我会有更多时间,所以我会以更好的方式完成它。
/*
* algorytm.cpp
* implementacja algorytmu
*
* Autor: Mateusz
*
*/
#include <cstdlib>
#include <iostream>
#include <string>
#include <stdio.h>
#include <fstream>
#include <vector>
#include "matryca_sub.h"
#include <sstream>
#include <istream>
#include <sstream>
using namespace std;
int ScoreMatrix::MainMatrix(char *mat_file, int x, int y)
{
cout << "Main matrix function start" << endl;
CreateMatrix(30);
ReadMatrix(mat_file);
int val;
val=GetValue(x, y);
return val;
cout << "Main matrix function end" << endl;
}
void ScoreMatrix::CreateMatrix(int edge)
{
cout << "Creating sub matrix start" << endl;
//int** scores = new int* [*edge-1];
//for (int i=0; i<=23; i++) scores[i] = new int[*edge-1];
if( scores != 0 ) delete [] scores;
scores = new int [edge*edge];
cols = edge;
cout << "Sub matrix created" << endl;
}
void ScoreMatrix::SetValue(int x, int y, int val)
{
cout << "write to sub matrix start" << endl;
//scores[x][y] = val;
scores[(cols* y) + x] = val;
cout << "write to sub matrix end" << endl;
}
int ScoreMatrix::GetValue(int x, int y)
{
//cout << "GetValue start" << endl;
//return scores[x][y];
return scores[(cols * x) + y];
cout << "GetValue end" << endl;
}
void ScoreMatrix::ReadMatrix(char *mat_file)
{
cout << "start reading matrix from file" << endl;
int row=0;
ifstream mfile;
mfile.open(mat_file);
mfile.precision(2);
mfile.setf(ios::fixed, ios::showpoint);
while(!mfile.eof())
{
for (row=0; row<=23; row++)
{
string line;
getline( mfile, line);
istringstream iss(line);
if (line[0] !='#' && line[0] != ' ')
{
int s;
iss >> s;
for (int i=1; !iss.eof(); i++)
{
iss >> s;
SetValue(i, row, s);
}
}
}
}
cout << "end reading matrix from file" << endl;
}
包含头文件:
/*
* File: mat_sub.h
* Author: mateusz
*
* Created on 6 luty 2011, 14:44
*/
#ifndef MAT_SUB_H
#define MAT_SUB_H
#include <cstdlib>
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
class ScoreMatrix
{
public:
ScoreMatrix():
cols (0)
,scores (0)
{}
char mat_file;
int MainMatrix(char *mat_file, int x, int y);
int GetValue(int x, int y);
private:
int cols, rows;
int* scores;
void CreateMatrix(int edge);
void SetValue(int x, int y, int val);
void ReadMatrix(char *mat_file);
};
#endif /* MAT_SUB_H */
在seagfoult函数打印许多开始从文件中读取矩阵
之一之前。
I need to read afile with matrix data and create matrix within my prrogram. The Matrix file format is looks similar to this:
# Matrix made by matblas from blosum62.iij
# * column uses minimum score
# BLOSUM Clustered Scoring Matrix in 1/2 Bit Units
# Blocks Database = /data/blocks_5.0/blocks.dat
# Cluster Percentage: >= 62
# Entropy = 0.6979, Expected = -0.5209
A R N D C Q E G H I L K M F P S T W Y V B Z X *
A 4 -1 -2 -2 0 -1 -1 0 -2 -1 -1 -1 -1 -2 -1 1 0 -3 -2 0 -2 -1 0 -4
R -1 5 0 -2 -3 1 0 -2 0 -3 -2 2 -1 -3 -2 -1 -1 -3 -2 -3 -1 0 -1 -4
N -2 0 6 1 -3 0 0 0 1 -3 -3 0 -2 -3 -2 1 0 -4 -2 -3 3 0 -1 -4
D -2 -2 1 6 -3 0 2 -1 -1 -3 -4 -1 -3 -3 -1 0 -1 -4 -3 -3 4 1 -1 -4
C 0 -3 -3 -3 9 -3 -4 -3 -3 -1 -1 -3 -1 -2 -3 -1 -1 -2 -2 -1 -3 -3 -2 -4
Q -1 1 0 0 -3 5 2 -2 0 -3 -2 1 0 -3 -1 0 -1 -2 -1 -2 0 3 -1 -4
E -1 0 0 2 -4 2 5 -2 0 -3 -3 1 -2 -3 -1 0 -1 -3 -2 -2 1 4 -1 -4
G 0 -2 0 -1 -3 -2 -2 6 -2 -4 -4 -2 -3 -3 -2 0 -2 -2 -3 -3 -1 -2 -1 -4
H -2 0 1 -1 -3 0 0 -2 8 -3 -3 -1 -2 -1 -2 -1 -2 -2 2 -3 0 0 -1 -4
I -1 -3 -3 -3 -1 -3 -3 -4 -3 4 2 -3 1 0 -3 -2 -1 -3 -1 3 -3 -3 -1 -4
L -1 -2 -3 -4 -1 -2 -3 -4 -3 2 4 -2 2 0 -3 -2 -1 -2 -1 1 -4 -3 -1 -4
K -1 2 0 -1 -3 1 1 -2 -1 -3 -2 5 -1 -3 -1 0 -1 -3 -2 -2 0 1 -1 -4
M -1 -1 -2 -3 -1 0 -2 -3 -2 1 2 -1 5 0 -2 -1 -1 -1 -1 1 -3 -1 -1 -4
F -2 -3 -3 -3 -2 -3 -3 -3 -1 0 0 -3 0 6 -4 -2 -2 1 3 -1 -3 -3 -1 -4
P -1 -2 -2 -1 -3 -1 -1 -2 -2 -3 -3 -1 -2 -4 7 -1 -1 -4 -3 -2 -2 -1 -2 -4
S 1 -1 1 0 -1 0 0 0 -1 -2 -2 0 -1 -2 -1 4 1 -3 -2 -2 0 0 0 -4
T 0 -1 0 -1 -1 -1 -1 -2 -2 -1 -1 -1 -1 -2 -1 1 5 -2 -2 0 -1 -1 0 -4
W -3 -3 -4 -4 -2 -2 -3 -2 -2 -3 -2 -3 -1 1 -4 -3 -2 11 2 -3 -4 -3 -2 -4
Y -2 -2 -2 -3 -2 -1 -2 -3 2 -1 -1 -2 -1 3 -3 -2 -2 2 7 -1 -3 -2 -1 -4
V 0 -3 -3 -3 -1 -2 -2 -3 -3 3 1 -2 1 -1 -2 -2 0 -3 -1 4 -3 -2 -1 -4
B -2 -1 3 4 -3 0 1 -1 0 -3 -4 0 -3 -3 -2 0 -1 -4 -3 -3 4 1 -1 -4
Z -1 0 0 1 -3 3 4 -2 0 -3 -3 1 -1 -3 -1 0 -1 -3 -2 -2 1 4 -1 -4
X 0 -1 -1 -1 -2 -1 -1 -1 -1 -1 -1 -1 -1 -1 -2 0 0 -2 -1 -1 -1 -1 -1 -4
* -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 1
Letters have constant place so since I am interested only in four of them I could assign x
and y
in program. What I need is only a matrix with values on which I could search for values by giving function 'GetValue' 'x' and 'y'.
Here is my code. It is only one part a class defined in header file, which contains and search through matrix for values. Maybe it is not elegant way to do that but I don't have so much time so right now I want to do it fast. Later I would have more time so I will do it in better way.
/*
* algorytm.cpp
* implementacja algorytmu
*
* Autor: Mateusz
*
*/
#include <cstdlib>
#include <iostream>
#include <string>
#include <stdio.h>
#include <fstream>
#include <vector>
#include "matryca_sub.h"
#include <sstream>
#include <istream>
#include <sstream>
using namespace std;
int ScoreMatrix::MainMatrix(char *mat_file, int x, int y)
{
cout << "Main matrix function start" << endl;
CreateMatrix(30);
ReadMatrix(mat_file);
int val;
val=GetValue(x, y);
return val;
cout << "Main matrix function end" << endl;
}
void ScoreMatrix::CreateMatrix(int edge)
{
cout << "Creating sub matrix start" << endl;
//int** scores = new int* [*edge-1];
//for (int i=0; i<=23; i++) scores[i] = new int[*edge-1];
if( scores != 0 ) delete [] scores;
scores = new int [edge*edge];
cols = edge;
cout << "Sub matrix created" << endl;
}
void ScoreMatrix::SetValue(int x, int y, int val)
{
cout << "write to sub matrix start" << endl;
//scores[x][y] = val;
scores[(cols* y) + x] = val;
cout << "write to sub matrix end" << endl;
}
int ScoreMatrix::GetValue(int x, int y)
{
//cout << "GetValue start" << endl;
//return scores[x][y];
return scores[(cols * x) + y];
cout << "GetValue end" << endl;
}
void ScoreMatrix::ReadMatrix(char *mat_file)
{
cout << "start reading matrix from file" << endl;
int row=0;
ifstream mfile;
mfile.open(mat_file);
mfile.precision(2);
mfile.setf(ios::fixed, ios::showpoint);
while(!mfile.eof())
{
for (row=0; row<=23; row++)
{
string line;
getline( mfile, line);
istringstream iss(line);
if (line[0] !='#' && line[0] != ' ')
{
int s;
iss >> s;
for (int i=1; !iss.eof(); i++)
{
iss >> s;
SetValue(i, row, s);
}
}
}
}
cout << "end reading matrix from file" << endl;
}
Header file included:
/*
* File: mat_sub.h
* Author: mateusz
*
* Created on 6 luty 2011, 14:44
*/
#ifndef MAT_SUB_H
#define MAT_SUB_H
#include <cstdlib>
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
class ScoreMatrix
{
public:
ScoreMatrix():
cols (0)
,scores (0)
{}
char mat_file;
int MainMatrix(char *mat_file, int x, int y);
int GetValue(int x, int y);
private:
int cols, rows;
int* scores;
void CreateMatrix(int edge);
void SetValue(int x, int y, int val);
void ReadMatrix(char *mat_file);
};
#endif /* MAT_SUB_H */
Before seagfoult function print one of many start reading matrix from file
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
天哪。如果我听起来很刺耳,希望您能原谅我,但是这段代码有很多问题,从基本设计一直到细节,例如您使用的循环从文件中读取数据。
首先,在我看来,您的“矩阵”类需要应用适量的“单一责任”。我会将其简化为二维矩阵,仅此而已。
其次,我会摆脱自己处理内存管理的麻烦。您真正需要的只是一个
std::vector
,以及足够的前端来提供 2D 寻址。第三,我会摆脱“getValue”/“setValue”,并使用下标,就像自 20 世纪 50 年代科学程序员从汇编语言转换为 Fortran 以来每一个精心设计的矩阵一样。
考虑到这些,我们得到一个简化的矩阵类,如下所示:
请注意,为简单起见,当您为其添加下标时,您大致像在 Fortran 或 BASIC 中一样使用
()
,而不是使用 < code>[] 就像您通常在 C 或 C++ 中所做的那样。您可以支持后者,但需要更多(而且更难看)函数中,这样我就摆脱了所有的
while (!whatever.eof()) 。
,因为从技术上讲,这些几乎肯定会不正确地工作,&line[1]
不能保证可以在 C++03 上工作,但 C++11 确实可以保证它。 ,主要是因为它适用于所有已知的实现。Oh my. I hope you'll forgive me if I sound harsh, but this code has a lot of problems, going all the way from the basic design all the way to the details of things like the loops you use to read data from the file.
First of all, it seems to me that your "matrix" class needs a healthy dose of "single responsibility" applied. I'd strip it down to being a 2D matrix, nothing else.
Second, I'd get rid of handling the memory management on your own. All you really need is a
std::vector
, with enough of a front-end to provide 2D addressing.Third, I'd get rid of the "getValue"/"setValue", and use subscripting like every decently-designed matrix since scientific programmers converted from assembly language to Fortran in the 1950s.
Taking those into account, we get a simplified matrix class, something like this:
Note that for simplicity, when you subscript this, you use
()
roughly like you would in Fortran or BASIC, rather than using[]
like you normally would in C or C++. You can support the latter, but it takes a lot more (and uglier) code. Also note that you do not wantcols * (y+x)
, you want(cols * y) + x
if you insist on using parentheses (though think it's pretty silly, given that the relative precedence of multiplication and addition is quite well known. Finally, I've made this a template simply because it's easy to do so -- if you want to specify the type directly, that's obviously pretty easy to do.Finally, I'd make reading the data from the file into a matrix into a free function. In doing so, I'd get rid of all the
while (!whatever.eof())
, because those are pretty much guaranteed to work incorrectly.Technically, the
&line[1]
isn't guaranteed to work C++03, but C++11 does guarantee it, largely because it works with all known implementations.这:
不是有效的 C++ 语法。你的意思是这个吗?:
同样,当你尝试访问
scores
的元素时,你应该这样做:顺便说一句,通过指针将
int
传递给函数是没有意义的如果您不打算修改原始值。我会将 CreateMatrix() 重写为:This:
is not valid C++ syntax. Do you mean this?:
Similarly, when you attempt to access element of
scores
, you should do something like:Incidentally, there's no point passing an
int
by pointer into a function if you don't intend to modify the original value. I would rewriteCreateMatrix()
as:(如果你的代码不起作用)读取此(且仅此)格式的AC代码:
(In case your code didn't work) A C code that reads this (and only this) format:
您似乎错误地使用了
[]
语法。我认为您需要更改以下行:注意
使用括号
()
而不是[]
。It looks like you are using the
[]
syntax incorrectly. I think you want change the following lines:to
Notice the use of parentheses
()
instead of[]
.要在其他人之前评估某些内容,请不要使用
[]
而是使用括号。它应该看起来像这样:To have something evaluated before others, you dont use
[]
but paranthesis. It should look like this: