如何转置非方矩阵?

发布于 2024-09-16 03:45:54 字数 172 浏览 5 评论 0原文

我有这段用于转置矩阵的代码:

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

它似乎适用于方阵,但不适用于非方阵。帮我!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

小草泠泠 2024-09-23 03:45:54

它适用于非方矩阵,但您必须确保 mat2 中的行数与 mat 中的列数匹配,反之亦然。即,如果 matNxM 矩阵,则 mat2 必须是 MxN 矩阵。

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. I.e., if mat is an NxM matrix, then mat2 must be an MxN matrix.

看轻我的陪伴 2024-09-23 03:45:54

这是您在应用程序中使用的实际代码吗?因为这是错误的。
for 语句的语法是:

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

在您的情况下,您应该使用类似这样的内容:

#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];
    }
}

这样就可以转置您的矩阵。
当然,这样你需要事先知道矩阵的维度。另一种方法可能是使用一些用户提供的数据动态初始化矩阵,如下所示:

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);
}

这样您将动态初始化矩阵,然后可以像以前一样对其进行转置。

Is that the actual code you have used in your application? Because it's wrong.
The syntax for the for statement is:

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.

瀟灑尐姊 2024-09-23 03:45:54

如果 col 是 mat2 中的行数(以及 mat 中的列数),而 row 是 mat2 中的列数(以及 mat 中的行数),那么这应该可行。

你需要一个额外的紧密卷发,但我假设你的代码中有这个。

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.

You need an extra close curly, but I assume you have that in your code.

信愁 2024-09-23 03:45:54

在 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;
  }
}

您可以替换为适当的标头和打印/扫描语句,以用 C 编写代码,或者从用户处获取输入并实现相同的代码。

Code for the transpose of a non square matrix with predefined dimensions in c++ would look something as follows :

#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.

情泪▽动烟 2024-09-23 03:45:54
void transpose(int arr[][10] , int rows ,int cols){
   int x = min(rows,cols);   //  minmum of rows or cols; 

   for(int i=0  ; i<rows ; i++){
       for(int j=0 ; j<cols ; j++){
        // first we will transpose the largest square matrix can form i.e(x X x) from given matrix 
        //,and then swap remaining elements

           if(i<x && j<x ){
               if(i<j)
               swap(arr[i][j] , arr[j][i]);
           }
           else {
               swap(arr[i][j] , arr[j][i]);
           }  
       }
   }  
} 
void transpose(int arr[][10] , int rows ,int cols){
   int x = min(rows,cols);   //  minmum of rows or cols; 

   for(int i=0  ; i<rows ; i++){
       for(int j=0 ; j<cols ; j++){
        // first we will transpose the largest square matrix can form i.e(x X x) from given matrix 
        //,and then swap remaining elements

           if(i<x && j<x ){
               if(i<j)
               swap(arr[i][j] , arr[j][i]);
           }
           else {
               swap(arr[i][j] , arr[j][i]);
           }  
       }
   }  
} 
呆头 2024-09-23 03:45:54
#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();
}
#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();
}
落叶缤纷 2024-09-23 03:45:54
  #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<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;  
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文