作者karcher (Grothendieck )
看板C_and_CPP
标题Re: [问题] 矩阵切割问题
时间Tue Jul 7 23:50:29 2009
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)
1F:推 hardcover:114? 弱到不行 07/13 15:09