简体   繁体   English

如何转置非方阵?

[英]How to Transpose a Non-Square Matrix?

I have this code for transposing a matrix:我有这个用于转置矩阵的代码:

for(j=0; j<col; j++) { 
    for(k=0; k<row; k++) {
        mat2[j][k] = mat[k][j];
    }

It seems to work on a square matrix but not on a nonsquare matrix.它似乎适用于方阵,但不适用于非方阵。 Help me!帮我!

It will work for a non-square matrix, but you have to ensure that the number of rows in mat2 matches the number of columns in mat , and vice versa.它适用于非方阵,但您必须确保mat2中的行数与mat的列数匹配,反之亦然。 Ie, if mat is an NxM matrix, then mat2 must be an MxN matrix.即,如果matNxM矩阵,则mat2必须是MxN矩阵。

Is that the actual code you have used in your application?这是您在应用程序中使用的实际代码吗? Because it's wrong.因为这是错误的。
The syntax for the for statement is: for语句的语法是:

for (Initialization; Condition to continue with the loop; Step Operation) {}

In your case you should use something like this:在你的情况下,你应该使用这样的东西:

#define COLS 10
#define ROWS 5

int mat[COLS][ROWS];
int mat2[ROWS][COLS];

int i, j;

for (i = 0; i < COLS; i ++) {
    for (j = 0; j < ROWS; j++) {
        mat2[j][i] = mat[i][j];
    }
}

This way this could transpose your matrix.这样就可以转置你的矩阵。 Naturally this way you need to know beforehand the dimensions for the matrix.自然地,通过这种方式,您需要事先知道矩阵的维度。 Another way could be to dinamically initialize your matrix by using some User provided data, like this:另一种方法是使用一些用户提供的数据动态初始化矩阵,如下所示:

int ** mat;
int ** mat2;

int cols, rows;
int i, j;

/* Get matrix dimension from the user */

mat = (int **) malloc (sizeof(int *) * cols);

for (i = 0; i < cols; i++) {
    mat[i] = (int *) malloc (sizeof(int) * rows);
}

This way you'll dinamically initialize a matrix and then you can transpose it the same way as before.这样你就可以动态地初始化一个矩阵,然后你可以像以前一样转置它。

Code for the transpose of a non square matrix with predefined dimensions in c++ would look something as follows :在 C++ 中转置具有预定义维度的非方阵的代码如下所示:

#include <iostream>

using namespace std;

int main()
{
int a[7][6],b[6][7],x,i,j,k;
/*Input Matrix from user*/
cout<<"Enter elements for matrix A\n";
for(i=0;i<7;i++)
    {for(j=0;j<6;j++)
       {
         cin>>a[i][j];
       }
    }
 /*Print Input Matrix A*/
 cout<<"MATRIX A:-"<<endl;
 for(i=0;i<7;i++)
    {for(j=0;j<6;j++)
       {
         cout<<a[i][j];
       }
       cout<<endl;
    }
/*calculate TRANSPOSE*/
for(i=0;i<7;i++)
    {for(j=0,k=5;j<6;j++,k--)
        {
         x=j+(k-j);
         b[x][i]=a[i][j];
        }
    }
/*Print Output Matrix B*/
cout<<"matrix B:-\n";
for(i=0;i<6;i++)
  {for(j=0;j<7;j++)
   {
       cout<<b[i][j];
   }
   cout<<endl;
  }
}

You can replace with the appropriate header and print/scan statements to write the code in C and alternatively take input from the user and implement the same.您可以用适当的标题和打印/扫描语句替换以用 C 编写代码,或者从用户那里获取输入并实现相同的内容。

If col is the number of rows in mat2 (and columns in mat) and row is the number of columns in mat2 (and rows in mat), then this should work.如果 col 是 mat2 中的行数(以及 mat 中的列数),而 row 是 mat2 中的列数(以及 mat 中的行数),那么这应该有效。

You need an extra close curly, but I assume you have that in your code.你需要一个额外的紧密卷曲,但我假设你的代码中有它。

  #include<conio.h>
  #include<ctype.h>
 #include<iostream.h>
 void trans(int [][10], int , int );
 int main()
 {
     int a[10][10],n,m,i,j;
  clrscr();
  cout<<"Enter the no. of rows and Columns: ";
  cin>>n>>m;
  cout<<"\n Enter the Matrix now:";
  for(i=0;i<n;i++)
   for(j=0;j<m;j++)
     cin>>a[i][j];  
  trans(a,n,m);
  getch();
  return 0;
 }
 void trans( int a[][10] , int n , int m )
 {  
   int i,b[10][10],j;
    for(i=0;i<n;i++)
     for(j=0;j<m;j++)
      { 
       b[j][i]=a[i][j];
      }
     cout<<"\n\nSize before Transpose "<<n<<"x"<<m<<"\n\n";
     for(i=0;i<m;i++)
       {
         for(j=0;j<n;j++)
          cout<<b[i][j]<<"\t";
     cout<<"\n";
      }
  cout<<"\n\nSize after transpose "<<m<<"x"<<n;  
   }
#include<conio.h>
#include<stdio.h>
main()
    int a[5][5],i,j,t;
    clrscr();
    for(i=1;i<=4;i++)
    { 
        for(j=1;j<=5;j++)
            scanf("%d",&a[i][j]);
    }

    for(i=1;i<=4;i++)
    { 
        for(j=i;j<=5;j++)
        {  
            if(i<=4&&j<=4)
            {
                t=a[j][i];
                a[j][i]=a[i][j];
                a[i][j]=t;
            }
            else
            a[j][i]=a[i][j];
        }
    }

    for(i=1;i<=5;i++)
    {
        for(j=1;j<=4;j++)
        {
            printf("%d ",a[i][j]);
        }
         printf("\n");
    }

    getch();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM