二维矩阵相乘 in C++
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <algorithm>
#include <functional>
#include <numeric>
template <class DataType>
void ReadMatFromFile(std::string &filename, std::vector<std::vector<DataType> > &lines_feat) {
std::ifstream vm_info(filename.c_str());
std::string lines;
DataType var;
std::vector<DataType> row;
lines_feat.clear();
while(!vm_info.eof()) {
getline(vm_info, lines);
if(lines.empty())
break;
std::stringstream stringin(lines);
row.clear();
while(stringin >> var) {
row.push_back(var);
}
lines_feat.push_back(row);
}
}
template <class T1, class T2>
int MatMultiply(const std::vector<std::vector<T1> > &Mata, const std::vector<std::vector<T2> > &Matb, std::vector<std::vector<T1> > &MatOut) {
if(Mata.at().size() != Matb.size()) {
std::cout<<"not match!\n";
return -;
}
for(size_t i=; i<Mata.size(); ++i) {
for(size_t j=; j<Matb.at().size(); ++j) {
std::vector<T2> col;
col.clear();
for(size_t k=; k<Matb.size(); ++k) {
col.push_back(Matb.at(k).at(j));
}
MatOut.at(i).at(j)=inner_product(Mata.at(i).begin(), Mata.at(i).end(), col.begin(), );
}
}
return ;
}
template <class T>
void Display2DVector(std::vector<std::vector<T> > &vv) {
for(size_t i=;i<vv.size();++i) {
for(typename::std::vector<T>::const_iterator it=vv.at(i).begin();it!=vv.at(i).end();++it) {
std::cout<<*it<<" ";
}
std::cout<<"\n";
}
std::cout<<"--------the total of the 2DVector is "<<vv.size()<<std::endl;
}
int main() {
std::string matfa="mata.dat", matfb="matb.dat";
std::vector<std::vector<int> > mata, matb;
ReadMatFromFile(matfa, mata);
Display2DVector(mata);
ReadMatFromFile(matfb, matb);
Display2DVector(matb);
std::cout<<mata.size()<<"\t"<<matb.at().size()<<std::endl;
std::vector<std::vector<int> > matout(mata.size(), std::vector<int>(matb.at().size()));
MatMultiply(mata, matb, matout);
Display2DVector(matout);
return ;
}
二维矩阵相乘 in C++的更多相关文章
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- IT公司100题-35- 求一个矩阵中最大的二维矩阵(元素和最大)
问题描述: 求一个矩阵中最大的二维矩阵(元素和最大).如: 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 中最大的是: 4 5 9 10 分析: 2*2子数组的最大和.遍历求和,时 ...
- [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵
11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...
- lintcode:搜索二维矩阵II
题目 搜索二维矩阵 II 写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每一列的整数从上到下是排序的. 在每一行或每一列中没 ...
- lintcode :搜索二维矩阵
题目: 搜索二维矩阵 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每行的第一个数大于上一行的最后一个整数. 样例 考虑下列矩阵: [ [1 ...
- Python小代码_5_二维矩阵转置
使用列表推导式实现二维矩阵转置 matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] print(matrix) matrix_t = [[ro ...
- LeetCode(74):搜索二维矩阵
Medium! 题目描述: 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 ...
- lintcode-28-搜索二维矩阵
搜索二维矩阵 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每行的第一个数大于上一行的最后一个整数. 样例 考虑下列矩阵: [ [1, 3, ...
随机推荐
- dubbo之主机绑定
主机绑定 查找顺序 缺省主机 IP 查找顺序: 通过 LocalHost.getLocalHost() 获取本机地址. 如果是 127.* 等 loopback 地址,则扫描各网卡,获取网卡 IP. ...
- Zabbix 监控磁盘IO
Zabbix 监控磁盘IO 1.数据获取脚本 #!/bin/bash # resource: http://www.muck.net/19/getting-hard-disk-performance- ...
- rrdtool 实践
rrdtool 实践 rrdtool 参数比较多,如果直接看文档,不知从何入手,直接从例子入手这样容易理解,模拟网卡流量 1. 创建数据库 rrdtool create Flow.rrd --star ...
- (转)淘淘商城系列——初始SolrCloud
http://blog.csdn.net/yerenyuan_pku/article/details/72944611 本文我只是简单介绍一下SolrCloud,如果大家要是感兴趣的话,可以参考Sol ...
- C# 获得固定年月日
/// <summary> /// 获得固定年月日,时和分不固定 : 2019-01-01 00:00:00 /// </summary> /// <returns> ...
- 9 Java 堆排序
堆是具有以下性质的完全二叉树,每个结点的值都大于或等于其左右孩子结点的值,称为大顶堆:或者每个结点的值都小于或等于其左右孩子结点的值,称为小顶堆.如下图: 同时,我们对堆中的结点按层进行编号,将这种逻 ...
- noip模拟赛 a
分析:f(n)就是问有多少对a*b*c = n,如果是Σf(i),那就是问有多少对a*b*c <= n. 这道题和之前做过的一道数三角形的题差不多:传送门,先假设一下a <= b < ...
- Docker在Windows 7下安装
前言&一些资料: Docker之父:Solomon Hykes Docker生日:2013年3月 Docker历史小故事 1.下载地址:(daocloud是一个中国的docker服务商,知不知 ...
- [bzoj4066/2683]简单题_KD-Tree
简单题 bzoj-4066 题目大意:n*n的棋盘,开始为均为0,支持:单点加权值,查询矩阵权值和,强制在线. 注释:$1\le n\le 5\cdot 10^5$,$1\le m \le 2\cdo ...
- 洛谷—— P1077 摆花
https://www.luogu.org/problem/show?pid=1077 题目描述 小明的花店新开张,为了吸引顾客,他想在花店的门口摆上一排花,共m盆.通过调查顾客的喜好,小明列出了顾客 ...