用C++从多个矩阵中创建一个矩阵

Create a matrix from multiple matrices in C++

本文关键字:一个 创建 C++      更新时间:2023-10-16

我有很多矩阵,例如

矩阵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

您可以使用矩阵类以自己的方式实现它。

相关文章: