1

私が作成したこのプログラムは、サイズに関係なく、行列を転置することになっています。ただし、想定どおりに機能していません。その理由はわかりません。コンパイル エラーはありませんが、{1,2,3}、{4,5,6} の出力は { 1,0,0}、{0,0,4}、これは私にはまったく意味がありません。私は紙に何度も「記憶のスナップショット」を書きましたが、欠けているものを見つけることができません。この時点で別の目が必要なだけです。

#include "stdafx.h"

#include <fstream>

#include <iostream>

#include <iomanip>

#include <string>

#include <cmath>

#include <vector>

using namespace std;

void squaretranspose(int &M, int &MT, int ROWS, int COLS);

int main(void)
{
    int M[2][3]={{1,2,3},{4,5,6}};
    int MT[3][2]={0};
    int ROWS(2),COLS(3);

int i,j;
cout << " The entries of the original matrix " << endl;
for(i=0;i<=ROWS-1;i++)
{
    for(j=0;j<=COLS-1;j++)
{
cout<<M[i][j]<<"\t";
}
cout << endl;
}
squaretranspose(M[0][0],MT[0][0],ROWS,COLS);
cout << " The entries of the transposed non-square matrix " << endl;
for(i=0;i<=COLS-1;i++)
{
    cout << endl;
    for(j=0;j<=ROWS-1;j++)
{
cout<<MT[i][j]<<"\t";
}
}
system ("PAUSE");
return 0;
}
void squaretranspose (int &M, int &MT, int ROWS, int COLS)
{
    // declare pointers to change the input matrice's values
    int *ptr,*ptrT;
// declare indices for a row by row process
    int i,j;
// declare placeholder 2d vectors for swapping the I,j, entries to ,j,i entries  
    vector < vector<int>> temp(ROWS,COLS);
    vector < vector<int>> tempT(COLS,COLS);
    vector < vector<int>> temp_T(ROWS,ROWS);
// set the pointers to point to the first entry of the input and output matrices
    ptr = &M;
    ptrT = &MT;

// if rows=cols we want to use 2d vector temp
    if (ROWS=COLS)
    {
// store all of the input matrice's values in the 2d vector "temp"
    for(i=0;i<=ROWS-1;i++)
    {
        for(j=0;j<=COLS-1;j++)
        {

// set the i,j th entry of the 2d vector "temp" equal to the value currently pointed to by the pointer
        temp[i][j]=*ptr;
// increment the pointer to the address of the next entry of the input matrix unless we are on the last entry
               if ((i!=ROWS-1)&&(j!=COLS-1))
               { 
               ptr++;
               }
        }
        }
    }
// reset pointer address to first entry
    ptr=&M;
// the for loop for swapping the j,i entries of the 2d vector "temp" with the i,j entries of the input matrix
    for(i=0;i<=ROWS-1;i++)
    {
        for(j=0;j<=COLS-1;j++)
        {
// if j is not equal to i swap the value pointed to by the pointer (the i,j entry of the input matrix) for the j,ith entries value of the 2d vector "temp"
        if (j!=i)
            {
            *ptr=temp[j][i];
            }
        // increment the pointer if it is not on the last entry
               if ((i!=ROWS-1)&&(j!=COLS-1))
               { 
               ptr++;
               }
        }
    }*/


// if ROWS<COLS we want to have 2d vector tempT
if (ROWS<COLS)
{
// store all of the input matrice's values in the 2d vector "tempT"
    for(i=0;i<=ROWS-1;i++)
    {
        for(j=0;j<=COLS-1;j++)
        {
// set the j,ith entry of the 2d vector "temp" equal to the value currently pointed to by the pointer
        tempT[j][i]=*ptr;
// increment the pointer to the address of the next entry of the input matrix
        if (((i!=(ROWS-1))&&(j!=(COLS-1))))
        {
            ptr++;
        }
        }
    }
    ptr=&M;
// transport the entries of tempT into the output matrix MT
    for(i=0;i<=COLS-1;i++)
    {
        for(j=0;j<=ROWS-1;j++)
        {
        *ptrT=tempT[i][j];
// increment the pointer
        if (((i!=ROWS-1)&&(j!=COLS-1)))
        {
            ptrT++;
        }
        }
    }
}
ptrT=&MT;

// if ROWS>COLS we want to use the 2d vector temp_T
if (ROWS>COLS)
{
// store all of the input matrice's values in the 2d vector "temp_T"
    for(i=0;i<=ROWS-1;i++)
    {
        for(j=0;j<=COLS-1;j++)
        {

// set the j,i th entry of the 2d vector "temp" equal to the value currently pointed to by the pointer
        temp_T[j][i]=*ptr;
// increment the pointer
        if (((i!=ROWS-1)&&(j!=COLS-1)))
        {
            ptrT++;
        }
    }

// the for loop for swapping the j,i entries of the 2d vector "temp" with the i,j entries of the input matrix
    for(i=0;i<=COLS-1;i++)
    {
        for(j=0;j<=ROWS-1;j++)
        {
// if j is not equal to i swap the value pointed to by the pointer (the i,j entry of the input matrix) for the j,ith entries value of the 2d vector "temp"
            if (j!=i)
            {
            *ptrT=temp_T[j][i];
            }
// increment the pointer
        if (((i!=ROWS-1)&&(j!=COLS-1)))
        {
            ptrT++;
        }
        }
    }
}
return;
}

// つまり、COLS が非常に小さく、中間の 2 次元の正方ベクトルのために ROWS が非常に大きいと言うと、あまり効率的ではありません。

4

2 に答える 2

0

コードにはいくつかの大きな問題があります。次の行です。

if (ROWS=COLS)

おそらく次のように意図されていました:

if (ROWS == COLS)

COLS最初のケースでは の値を2 番目のケースに割り当て、ROWSそれらが等しいかどうかをチェックします。使用する必要があるときに使用しているすべてのforループで、そうでない場合は、配列の境界外にアクセスすることになります。<=<

それを超えて、コードは複雑すぎます。転置関数は非常に単純である必要があります。これは可能な方法の 1 つです。

template <int n, int m>
void squaretranspose( int a[n][m], int b[m][n])
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            b[j][i] = a[i][j];
        }
    }
} 

しかし、行列を転置する最も速くて簡単な方法は、座標を反転する(i,j)ことです(j,i)。一方、パフォーマンスが主な関心事である場合は、この前のスレッドで私の解決策もかなりうまくカバーされており、あなたに合った他の解決策として私の解決策があります。

于 2013-07-02T23:50:19.427 に答える