Enb0U 2021-08-04 10:54 采纳率: 83.3%
浏览 82
已结题

C++如何实现删除矩阵中多行多列



```c++

faulttestM = { {1,1,1,0,0,1,0,1,0,0},
              {0,0,0,1,0,0,1,1,0,1},
              {0,0,1,0,1,1,1,1,1,1},
              {0,0,0,1,0,0,1,1,0,1},
              {1,1,1,0,0,1,0,1,0,0} };

如上矩阵,需要删除相同的行和列,请问该怎么写代码,我自己写的只能删除一行T T


    vector <int> reRow;
    vector <int> reCol;
    int b = 0;
    //查找相同的行,放入reRow数组中
    for (int i = 0; i < row; i++)
    {

        for (int count1 = i + 1; count1 < row; count1++)
        {
            int a = 0;
            for (int j = 0; j < rol; j++)
            {
                if (faulttestM[i][j] == faulttestM[count1][j])
                {
                    a++;
                    continue;
                }
                else if (faulttestM[i][j] != faulttestM[count1][j])
                {
                    break;
                }
            }
            if (a == rol)
            {
                b++;
                if (count(reRow.begin(), reRow.end(), count1))
                {
                    break;
                }
                else
                {
                    reRow.push_back(count1);
                }

            }
        }
    }
    cout << "检查re中是否存在数值:" << reRow[0] << endl;
    cout << "check the size of reRow:" << reRow.size() << endl;
    //查找相同的列,放入reCol数组中
    int c = 0;
    for (int j = 0; j < rol; j++)
    {

        for (int count2 = j + 1; count2 < rol; count2++)
        {
            int a = 0;
            for (int i = 0; i < row; i++)
            {
                if (faulttestM[i][j] == faulttestM[i][count2])
                {
                    a++;
                    continue;
                }
                else if (faulttestM[i][j] != faulttestM[i][count2])
                {
                    break;
                }
            }
            if (a == row)
            {
                c++;
                if (count(reCol.begin(), reCol.end(), count2))
                {
                    break;
                }
                else
                {
                    reCol.push_back(count2);
                }
            }
        }
    }
    cout << "check the size of reCol:" << reCol.size() << endl;

    int optrow = row - reRow.size();
    int optrol = rol - reCol.size();

    cout << "row : " << row << endl;
    cout << "rol : " << rol << endl;
    cout << "optrow : " << optrow << endl;
    cout << "optrol : " << optrol << endl;



    int** optftmatrix;
    //矩阵初始化
    optftmatrix = new int* [row];
    for (int i = 0; i < row; i++)
        optftmatrix[i] = new int[rol];

    for (int i = 0; i < row; i++)
    {

        for (int j = 0; j < rol; j++)
        {
            optftmatrix[i][j] = 0;
        }

    }//初始化结束

    for (int k = 0; k < reRow.size(); k++)
    {
        cout << "reRow里的值:" << reRow[k] << endl;
    }

    for (int i = 0; i < reCol.size(); i++)
    {
        cout << "reCol里的值:" << reCol[i] << endl;
    }
    cout << "检查矩阵中的值:"<<faulttestM[0][5]<<" "<<faulttestM[1][3] << endl;

    int r0 = 0, c0 = 0;
    for (int i = 0; i < row; i++)
    {
        for (int k = 0; k < reRow.size(); k++)
        {
            if (i == reRow[k])
            {
                r0 = 1;
            }
            else
            {
                r0 = 0;
            }
            int delColCount = -1;
            for (int j = 0; j < rol; j++)
            {

                for (int p = 0; p < reCol.size(); p++)
                {

                    if (j < reCol[p])
                    {
                        c0 = 0;

                    }
                    else
                    {

                        c0 = 1;

                        if (j + 1 == rol)
                        {
                            continue;
                        }
                    }
                    /*for (int k = 1; k < reCol.size(); k++)
                    {
                        if (reCol[k - 1] < j < reCol[k])
                        {
                            delColCount += 1;
                        }
                        else if (reCol[k] < j < reCol[k + 1])
                        {
                            delColCount += 2;
                        }*/

                    //}
                    optftmatrix[i][j] = faulttestM[i + r0][j + c0];
                }
            }

        }
    }

```

  • 写回答

2条回答 默认 最新

  • 八云黧 2021-08-04 11:05
    关注

    给个思路:首先保证reRow和reCol正确无误
    然后有伪码如下:

    长=0
    for i in 原数组长:
        if(i in reRow):
            continue
        宽=0
        for j in 原数组宽:
            if(j in reCol):
                continue
            新数组[长][宽++]=旧数组[i][j]
        长++
            
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月13日
  • 已采纳回答 8月5日
  • 创建了问题 8月4日

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题