前言
今天周六,时间多,写了一会,发现之前写的reshape有点问题,本次更新就是定义了数组以及矩阵,数组的四则运算,以及矩阵的乘法,并且定义单个指针的是数组,两个指针的是矩阵,完成了矩阵的转置,指针来指针去,把我搞糊涂了。。。
代码
/* 作者:山科_xxin 时间:2017-03-18 23:14:51 功能:矩阵转置,数组运算,矩阵乘法 类别:C++矩阵,数组练习 */ #include<iostream> #include<stdlib.h> using namespace std; typedef int** Matt; typedef int* Arrr; template <class T> int *getArrayLen(T& array) { int len; int row; int col; int len1; int Len[3]; len = sizeof(array)/sizeof(int); col = sizeof(array[0])/sizeof(int); row = len/col; Len[0] = len; Len[1] = row; Len[2] = col; return Len; } class Matrix { public: int visitM(int **Mat,int row,int col); int **Trans(int *Mat,int row,int col); int **mult(int **Mat,int **Mat2,int row1,int col1,int row2,int col2); }; int Matrix::visitM(int **Arr,int row,int col) { for(int i = 0;i<row;i++) { for(int j = 0;j<col;j++) { cout<<Arr[i][j]<<" "; } cout<<endl; } return 0; } int **Matrix::Trans(int *Mat,int row,int col) { int **Mat2 = new int*[col]; for(int i = 0;i<col;i++) Mat2[i] = new int[row]; for(int i=0;i<row;i++) { for(int j = 0;j<col;j++) { Mat2[j][i] = Mat[i*col+j]; } } return Mat2; } int **Matrix::mult(int **Mat1,int **Mat2,int row1,int col1,int row2,int col2) { int **Mat3 = new int*[100]; for(int i = 0;i<100;i++) Mat3[i] = new int[100](); //int value = 0; if (col1!=row2) { cout<<"输入矩阵非法"<<endl; exit(0); } else { for(int i = 0;i<row1;i++) { for (int j = 0;j<col2;j++) { for(int k = 0;k<col1;k++) { Mat3[i][j] += Mat1[i][k]*Mat2[k][j]; } } } } return Mat3; } class ArrT { public: int visitA(int *Arr,int row,int col); int **Z(int *Arr,int row,int col); int *Sum(int *Arr,int row,int col); int **arrmul(int *Arr1,int *Arr2,int row,int col); int **arrsub(int *Arr1,int *Arr2,int row,int col); int **arrmult(int *Arrl,int *Arr2,int row,int col); }; int **ArrT::Z(int *Arr,int row,int col) { int **Mat2 = new int*[row]; for(int i = 0;i<row;i++) Mat2[i] = new int[col]; for(int i = 0;i<row;i++) { for(int j = 0;j<col;j++) Mat2[i][j] = Arr[i*col+j]; } return Mat2; } int ArrT::visitA(int *Arr,int row,int col) { for(int i = 0;i<row;i++) { for(int j = 0;j<col;j++) { cout<<Arr[i*col+j]<<" "; } cout<<endl; } return 0; } int *ArrT::Sum(int *Arr,int row,int col) { int he = 0; int *herow = new int[100]; *herow = 0; int value = 0; for (int i = 0;i<row;i++) { for(int j = 0;j<col;j++) { herow[i] += *(Arr+j); } } return herow; } int **ArrT::arrmul(int *Arr1,int *Arr2,int row,int col) { int **Arr3 = new int*[row]; for(int i = 0;i<row;i++) Arr3[i] = new int[col]; for(int i = 0;i<row;i++) for(int j = 0;j<col;j++) { Arr3[i][j] = Arr1[i*col+j]+Arr2[i*col+j]; } return Arr3; } int **ArrT::arrmult(int *Arr1,int *Arr2,int row,int col) { int **Arr3 = new int*[row]; for(int i = 0;i<row;i++) Arr3[i] = new int[col]; for(int i = 0;i<row;i++) for(int j = 0;j<col;j++) { Arr3[i][j] = Arr1[i*col+j]*Arr2[i*col+j]; } return Arr3; } int **ArrT::arrsub(int *Arr1,int *Arr2,int row,int col) { int **Arr3 = new int*[row]; for(int i = 0;i<row;i++) Arr3[i] = new int[col]; for(int i = 0;i<row;i++) for(int j = 0;j<col;j++) { Arr3[i][j] = Arr1[i*col+j]-Arr2[i*col+j]; } return Arr3; } int main() { ArrT arr; Matrix mat; Arrr len; int row; int col; Arrr sum; Matt mul; Matt T; Matt arrz; Matt result; Matt c; int a[2][3] = {{1,2,3},{4,5,6}}; int b[2][3] = {{2,6,9},{2,6,4}}; len = getArrayLen(a); row = len[1]; col = len[2]; cout<<"行:"<<row<<" "<<"列:"<<col<<endl; cout<<"原数组:"<<endl; arr.visitA(*a,row,col); mul = arr.arrmul(*a,*b,row,col); arrz = arr.Z(*a,row,col); T = mat.Trans(*a,row,col); cout<<"转置后:"<<endl; mat.visitM(T,col,row); cout<<"转置后矩阵*原矩阵"<<endl; result = mat.mult(T,arrz,col,row,row,col); mat.visitM(result,col,col); c = arr.arrmult(*a,*b,row,col); cout<<"a数组*b数组"<<endl; mat.visitM(c,row,col); system("pause"); return 0; }
后语
有了这些基本操作的函数,就可以安心写算法了。明天研究Opencv 仿射变换。