用C++从多个矩阵中创建一个矩阵
Create a matrix from multiple matrices in C++
我有很多矩阵,例如
矩阵1:
2 3
4 5
矩阵2:
7 6
4 1
等等。。。
如果我有4个这样的数组,有没有一种方法可以将每个数组分配给另一个数组的一个元素来创建一个大数组,例如
矩阵:
2 3 7 6
4 5 4 1
…
我真的很感激在这件事上能得到任何帮助。
感谢
设CCD_ 1为4给定矩阵,以下是将从上述4矩阵创建所需矩阵Mat5
的代码,没有特定的逻辑,您只需要找出哪个元素应该放在哪里
int Mat1[][]={{2,3},{4,5}};
int Mat2[][]={{6,7},{8,9}};
int Mat3[][]={{1,2},{3,4}};
int Mat4[][]={{5,6},{7,8}};
int Mat5[][]=new int[4][4];
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
//inserting first 2 matrices
Mat5[i][j]=Mat1[i][j];
Mat5[i][j+2]=Mat2[i][j];
//inserting last 2 matrices
Mat5[i+2][j]=Mat3[i][j];
Mat5[i+2][j+2]=Mat4[i][j];
}
}
输出:
2 3 6 7
4 5 8 9
1 2 5 6
3 4 7 8
我不知道你想如何处理矩阵。但我会使用矢量:
std::vector<std::vector<double> > mat1(2, std::vector<double>(2)); // 2x2 Matrix
mat1[0][0] = 2;
mat1[0][1] = 3;
mat1[1][0] = 4;
mat1[1][1] = 5;
上面的代码代表您的矩阵1。你可以对其他矩阵做同样的事情。下一步是创建组合矩阵:
std::vector<std::vector<double> > combined;
combined.push_back(mat1[0]);
combined.push_back(mat1[1]);
combined.push_back(mat2[0]);
combined.push_back(mat2[1]);
combined.push_back(mat3[0]);
combined.push_back(mat2[1]);
得到的矩阵combined
看起来像
2 3
4 5
7 6
4 1
如果你想更灵活,你也可以把所有矩阵放在一个向量中(即,`std::vector>>)。
此代码要求您具有任何类型的Matrix class
,但它是通用的,适用于所有变体(您可以连接任意数量的完美平方数矩阵):
#include <iostream>
#include <vector>
#include "custom_matrix.hpp"
bool is_perfect_square(long long x)
{
long long dx=std::sqrt((long double)x);
if(x==(dx*dx))
return true;
else
return false;
}
template<class T>
Matrix<T> join_matrixes(const std::vector<Matrix<T>>& parts, size_t level)
{
if(!is_perfect_square(parts.size()))
throw; // check if amount of matrixes is correct (1,4,9 etc)
if(std::sqrt(parts.size()) != level)
throw; // check if amount of matrixes equals given level of combining
if(std::find_if(parts.begin(), parts.end(), [&parts](auto x){return !x.is_equal_size(parts.front());}) != parts.end())
throw; //check if every matrix is equal in size to each other
Matrix<T> result(level*parts[0].size_x(), level*parts[0].size_y());
for(Size y = 0; y < level; ++y)
{
for(Size x = 0; x < level; ++x)
{
for(Size inner_y = 0; inner_y < parts[0].size_y(); ++inner_y )
{
for(Size inner_x = 0; inner_x < parts[0].size_x(); ++inner_x )
{
result[y*parts[0].size_y()+inner_y][x*parts[0].size_x()+inner_x] = parts[y*level+x][inner_y][inner_x];
}
}
}
}
return result;
}
int main()
{
std::cout << join_matrixes(std::vector<Matrix<float>>
{Matrix<float>(3,5,3.14),
Matrix<float>(3,5,69.69),
Matrix<float>(3,5,13.7),
Matrix<float>(3,5,2.71)}, 2) << std::endl;
std::cout << join_matrixes(std::vector<Matrix<int>>
{Matrix<int>(2,2,1),
Matrix<int>(2,2,4),
Matrix<int>(2,2,9),
Matrix<int>(2,2,16),
Matrix<int>(2,2,25),
Matrix<int>(2,2,36),
Matrix<int>(2,2,49),
Matrix<int>(2,2,64),
Matrix<int>(2,2,81)}, 3) << std::endl;
}
输出:
3.140 3.140 3.140 69.690 69.690 69.690
3.140 3.140 3.140 69.690 69.690 69.690
3.140 3.140 3.140 69.690 69.690 69.690
3.140 3.140 3.140 69.690 69.690 69.690
3.140 3.140 3.140 69.690 69.690 69.690
13.700 13.700 13.700 2.710 2.710 2.710
13.700 13.700 13.700 2.710 2.710 2.710
13.700 13.700 13.700 2.710 2.710 2.710
13.700 13.700 13.700 2.710 2.710 2.710
13.700 13.700 13.700 2.710 2.710 2.710
1 1 4 4 9 9
1 1 4 4 9 9
16 16 25 25 36 36
16 16 25 25 36 36
49 49 64 64 81 81
49 49 64 64 81 81
您可以使用矩阵类以自己的方式实现它。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 试图在visual studio上用C++创建一个桌面应用程序
- 如何创建一个空的全局类并在启动时实例化它
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何创建一个QTableWidgetItem,用长文本右对齐,左边有省略号
- 我正在尝试使用 c++ 创建一个货币转换程序,我不知道如何继续
- visual是否可以在c++中创建一个接收无限数量相同类型(或至少相当数量)参数的函数
- C++-我可以创建另一个类的成员并在构造函数中使用它吗
- 试图创建一个多线程程序来查找0-100000000之间的总素数
- 如何创建一个函数来计算并返回平均值、最大值和最小值
- 如何使用CLion在Mac上创建一个新的.txt文件
- 如何在没有数据拷贝的情况下从指针创建一个Eigen VectorXd对象
- 在c++中为double类型的数组创建一个unique_ptr
- 如何创建函数管道,以便函数一个接一个地运行?
- 如何从这些语句中的每一个创建不同的函数?
- 给定一个创建的带有货物的链表,我需要使用函数检查实际序列或"train"是否有效
- 一个创建彩票游戏的程序,该彩票游戏可以有效地创建 10 套而没有任何重复?
- 如何实现一个创建新对象并返回对它的引用的C++方法
- 如何成功创建一个创建对象并返回指向该对象的指针的函数