从文件加载矩阵

2024-01-23

我需要读取包含矩阵数据的文件并在程序中创建矩阵。 Matrix 文件格式类似于:

#  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 and 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 函数打印众多内容之一之前start reading matrix from file.


天啊。如果我听起来很严厉,希望您能原谅我,但是这段代码有一个lot问题的全部内容,从基本设计一直到细节,例如用于从文件中读取数据的循环。

首先,在我看来,您的“矩阵”类需要应用适量的“单一责任”。我会将其简化为二维矩阵,仅此而已。

其次,我会摆脱自己处理内存管理的麻烦。您真正需要的是std::vector,有足够的前端来提供 2D 寻址。

第三,我会摆脱“getValue”/“setValue”,并使用下标,就像自 20 世纪 50 年代科学程序员从汇编语言转换为 Fortran 以来每一个精心设计的矩阵一样。

考虑到这些,我们得到一个简化的矩阵类,如下所示:

template <class T>
class matrix { 
    std::vector<T> data;
    size_t cols;
public:
    matrix(int x, int y) : cols(x), data(x*y) {}
    T &operator()(int x, int y) { 
         return data[cols * y + x];
    }
};

请注意,为简单起见,当您为其添加下标时,您使用()大致就像在 Fortran 或 BASIC 中一样,而不是使用[]就像您通常在 C 或 C++ 中所做的那样。你可以支持后者,但这需要更多(而且更丑陋)code https://stackoverflow.com/questions/2216017/dynamical-two-dimension-array-according-to-input/2216055#2216055。另请注意,您这样做not want cols * (y+x), 你要(cols * y) + x如果您坚持使用括号(尽管认为这很愚蠢,因为乘法和加法的相对优先级是众所周知的。最后,我将其作为模板只是因为这样做很容易 - 如果您想指定直接输入类型,这显然很容易做到。

最后,我将把文件中的数据读取到矩阵中,变成一个自由函数。这样做的话,我会摆脱所有while (!whatever.eof()),因为这些几乎肯定会错误地工作。

template <class T>
void read_matrix(std::string const &filename, matrix<T> &m) { 
    std::ifstream infile(filename);
    std::string line;
    int x = 0, y=0;

    while (std::getline(infile, line)) {
        if (line[0] == '#' || line[0] == ' ')
            continue;

        int value;
        std::istringinstream converter(&line[1]); // &line[1] to skip leading letter
        while (converter >> value)
            m(x++, y) = value;
        ++y;
    }            
}

从技术上讲,&line[1]不保证可以在 C++03 上工作,但 C++11 确实可以保证,主要是因为它可以与所有已知的实现一起工作。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从文件加载矩阵 的相关文章

随机推荐

  • 增加火花任务大小[重复]

    这个问题在这里已经有答案了 当我在 Spark Shell 中执行代码时遇到问题 Stage 1 gt 0 0 16 17 01 13 06 09 24 WARN TaskSetManager Stage 1 contains a task
  • 如何处理“超出最大存储过程、函数、触发器或视图嵌套级别(限制 32)”。

    我被要求创建脚本 希望运行它的人提供员工 ID 找到所提供的员工任意深度监督的所有员工 我的代码是 CREATE FUNCTION dbo GetNames V uniqueidentifier RETURNS OldNames TABLE
  • 为什么 R 的重复数据在排序数据上表现更好?

    在比较答案中两个函数的效率时检查列表是否包含 R 中的另一个列表 https stackoverflow com a 39350733 4408538 我偶然发现了一个有趣的结果 排序大大提高了效率duplicated当向量很大时 这让我感
  • 在 PropertyGrid 中设置只读属性将所有属性设置为只读

    我正在使用一个PropertyGrid控件来编辑我的类属性 并且我尝试根据其他属性设置将某些属性设置为只读 这是我班级的代码 Imports System ComponentModel Imports System Reflection P
  • Redis 和 Node.js 以及 Socket.io 问题

    我刚刚学习 redis 和 node js 有两个问题我找不到任何令人满意的答案 我的第一个问题是关于在 Node js 中重用 Redis 客户端 我找到了这个问题和答案 如何在socket io中重用redis连接 https stac
  • 比较两个数组并找出差异

    我需要比较两个数组并获得差异 背景 第一个数组将列出文件夹中的文件 第二个数组将读取文件的内容并存储在数组中 第一个数组的输出将是 a b c d e 第二个数组的输出将是 a b c e 我如何比较这两个有差异的数组 我想要的最终输出是
  • java数组循环遍历

    我有一个包含 1 2 3 4 5 值的数组 array a 1 2 3 4 5 现在我想以循环方式遍历它 就像我想打印 2 3 4 5 1 或 3 4 5 1 2 或 5 1 2 3 4 等等 任何算法关于这个 Edit 我想以循环方式打印
  • 后台服务科尔多瓦离子应用程序。 Backgroudn 插件无法在 ios 8.3 上运行

    我想实现一个后台服务 将地理位置发送到服务器 因此我使用了插件 cordova plugin background modehttps github com katzer cordova plugin background mode htt
  • asyncio - 重新引发任务异常

    我正在使用 asyncio 进行一些 TCP 通信 我有一个Receive 函数的作用是read 无限循环中 这作为后台任务运行 使用asyncio create task Receive 现在 如果连接被对等方关闭 则会引发异常 或者可能
  • 增强数据结构而不浪费内存

    我有课Tree我想将其扩充为更专业的数据结构 例如Order tree and Interval tree 这些增强功能需要添加Node 例如大小信息 以及对某些算法的微小更改 我想知道在 C 中实现性能 可读性和可维护性方面的增强的最佳方
  • Quickbooks API - 如何使用存储的信用卡创建费用

    我想知道如何使用存储的信用卡创建费用 使用 card api 我已成功将卡存储在 QuickBooks Online 中并收到返回的卡 ID 现在我想对存储的卡进行收费 为了创建费用 我发现有两种提供卡详细信息的方法 提供明确的卡详细信息
  • 变换路径和重绘不起作用

    我有一次创建的路径 并向其中添加了 React 在某些情况下 我将路径偏移Path offset or Path transform 然后使我的画布无效以重新绘制路径 但路径并没有在新的地方重新绘制 我通过使用检查了路径边界Path com
  • 如何在通过curl触发构建时设置Jenkins构建描述?

    我正在尝试设置我正在触发的构建的构建描述 因为我正在启动构建 但到目前为止我还没有运气 我遇到了一个解决方案 将文本添加到 Jenkins 远程 API 触发的构建页面 https stackoverflow com questions 1
  • 在 Linq Select 中创建元组

    我正在使用 C 和 NET Framework 4 5 1 使用 Entity Framework 6 1 3 从 SQL Server 数据库检索数据 我有这个 codes codesRepo SearchFor predicate Se
  • 使用 ParamArray,但需要至少一个参数

    我曾经拥有的 Public Sub Subscribe channel As ChannelType Public Sub Subscribe channels As IEnumerable Of ChannelType 第一个只是调用第二
  • 检测节点何时被删除(或因为父节点被删除而从 DOM 中删除)

    我想检测节点 例如nodeX 何时不再可用 要么是因为它被删除了 要么是因为它的父节点 或其父节点 被删除了 到目前为止 我能想到的就是使用Mutation Observer来查看页面上的任何删除 并检查删除的节点是否是nodeX或有nod
  • #!/usr/bin/python 和#!/usr/bin/env python,哪个支持?

    Python 脚本的 shebang 应该是什么样的 有些人支持 usr bin env python因为它可以智能地找到Python解释器 其他人支持 usr bin python 因为现在在大多数 GNU Linux 发行版中pytho
  • 单处理器环境可以防止竞争条件吗?

    当多个处理器工作时 进程是并发工作的 当多个线程访问某个公共数据区域时 就会发生竞争条件 一个线程可能会覆盖另一个值 那么 如果是单处理器 单核环境 是否可以防止竞争条件的发生呢 帮我解答一下这个困惑 谢谢 单处理器环境中可能会发生竞争条件
  • 多个客户端的异步 TCP 服务器

    我有一个 TCP 服务器 它异步侦听传入连接 如果只有一个客户端连接 则一切正常 但如果有两个或多个连接 服务器不会收到第一条消息 当我调试 ReceiveCallback 函数时 我可以看到服务器获取消息的长度 但没有获取数据 IE 如果
  • 从文件加载矩阵

    我需要读取包含矩阵数据的文件并在程序中创建矩阵 Matrix 文件格式类似于 Matrix made by matblas from blosum62 iij column uses minimum score BLOSUM Cluster