Re: [問題] 矩陣切割問題
看板C_and_CPP (C/C++)作者karcher (Grothendieck )時間16年前 (2009/07/07 23:50)推噓1(1推 0噓 0→)留言1則, 1人參與討論串2/2 (看更多)
y※ 引述《sin123 (GOGOGO)》之銘言:
: 我的A矩陣沒辦法切割成四等份(2的倍數),
: 請問是哪邊出問題?
1.不太熟指標、動態配置、pass by address/reference
2.使用一個未定義結構
: 要怎麼思考比較好??
#define N 4 //N*N 矩陣
#define MAX 4 //分配亂數值0~3
: void randV(int Matrix[N][N]); //分配矩陣內容亂數值 函式
: void printArray(int Matrix[][]); //印出矩陣內容 函式
: //void smallMatrix(Matrix_t A, int N,Matrix_t p11, Matrix_t p12, Matrix_t p21, Matrix_t p22);
: //Matrix_t Matrix(int N);
~~~~~~~~~~~
未知的結構??假設這位114的板友不想用內建型態
那這裡就使用一個自訂義的結構說明看看
這樣或許比較有幫助
struct matrix
{
double * data;
unsigned int size ;
matrix(unsigned int n)
{
size = n;
data = new double[size*size];
memset(data , 0 , (size*size)*sizeof(double));
}
double & operator()(int i,int j)
{
return *(data+size*i+j);
}
// Matrix & operator=(const matrix &);
} ;
: void smallMatrix(Matrix_t A, int N,
: Matrix_t p11, Matrix_t p12, Matrix_t p21, Matrix_t p22)
: {
: int N1 = N/2,
: row,col; // loop-indexes
: for (row=0; row<N1; row++) //read data from p[0][0] ... p[N1-1][N1-1]
: for (col=0; col<N1; col++)
: p11[row][col] = A[row][col];
............................同樣的迴圈寫四次
: for (row=0; row<N1; row++) //read data from p[0][N1] ... p[N1-1][N-1]
: for (col=0; col<N1; col++)
: p12[row][col] = A[row][col+N1];
~~~~~~~
: for (row=0; row<N1; row++) // read data from p[N1][0] ... p[N-1][N1-1]
: for (col=0; col<N1; col++)
: p21[row][col] = A[row+N1][col];
~~~~~
: for (row=0; row<N1; row++) // read data from p[N1][N1] ... p[N-1][N-1]
: for (col=0; col<N1; col++)
: p22[row][col] = A[row+N1][col+N1];
~~~~~~~~~~~~~~
: }
int N1 = N/2;
假設matrix A(N);
matrix p(N1); matrix p2(N1); ..... , matrix p4(N1);
struct point
{
int row;
int col;
};
可以試著找個陣列將開始讀取的地方記下來
point inital_point [] ={ 0,0,{0,N1},{N1,0},{N1,N1}}
然後進行下面的函式
void smallMatrix( matrix & A , matrix & p , point initial_point)
{
if(A.size < p.size || A.size % p.size != 0)
{
std::cout << "Fatal Error" <<"\n";
return;
}
int N1 = p.size;
for (int row=0; row<N1; row++)
for (int col=0; col<N1; col++)
p(row,col) = A(row+ initial_point.row,col+ initial_point.col);
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.137.82.145
※ 編輯: karcher 來自: 220.137.82.145 (07/07 23:52)
※ 編輯: karcher 來自: 220.137.82.145 (07/07 23:54)
※ 編輯: karcher 來自: 220.137.82.145 (07/07 23:56)
※ 編輯: karcher 來自: 220.137.82.145 (07/08 00:03)
※ 編輯: karcher 來自: 220.137.82.145 (07/08 00:04)
推
07/13 15:09, , 1F
07/13 15:09, 1F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章