首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C++中从多个矩阵创建矩阵

在C++中从多个矩阵创建矩阵
EN

Stack Overflow用户
提问于 2016-03-30 21:39:16
回答 3查看 1.4K关注 0票数 1

我有许多矩阵,例如:

矩阵1:

2 3

4 5

矩阵2:

7 6

4 1

以此类推。

如果我有4个这样的数组,有没有一种方法可以将每个数组分配给另一个数组的元素,以创建一个大的数组,例如

矩阵:

2 3 7 6

4 5 4 1

..。

我真的很感激在这件事上的任何帮助。

谢谢

EN

回答 3

Stack Overflow用户

发布于 2016-03-30 22:57:58

假设Mat1,Mat2,Mat3,Mat4是给定的4矩阵,下面是从上面提到的4矩阵创建所需矩阵Mat5的代码,没有特定的逻辑,您只需计算出哪个元素应该放在哪里!!

代码语言:javascript
复制
    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

票数 1
EN

Stack Overflow用户

发布于 2016-03-30 22:02:26

我不知道您想如何处理矩阵。但我会使用向量:

代码语言:javascript
复制
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。你可以对其他矩阵做同样的事情。下一步是创建组合矩阵:

代码语言:javascript
复制
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将如下所示

代码语言:javascript
复制
2 3
4 5
7 6
4 1

如果你想更灵活,你也可以把所有的矩阵放到一个向量中(例如,` `std::vector > >)。

票数 0
EN

Stack Overflow用户

发布于 2016-03-31 02:33:27

这段代码要求您拥有任何类型的Matrix class,但它是通用的,并且适用于所有变体(您可以连接任意数量的完全平方数的矩阵):

代码语言:javascript
复制
#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;
}

输出:

代码语言:javascript
复制
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

你可以用你的矩阵类来实现它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36310580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档