Tag: 矩阵

C中的优化矩阵乘法

我正在尝试比较矩阵乘法的不同方法。 第一个是常规方法: do { for (j = 0; j < i; j++) { for (k = 0; k < i; k++) { suma = 0; for (l = 0; l < i; l++) suma += MatrixA[j][l]*MatrixB[l][k]; MatrixR[j][k] = suma; } } } c++; } while (c<iteraciones); 第二个包括首先转置矩阵B然后按行进行乘法运算: int f, co; for (f = 0; f […]

在C中分配矩阵

我想分配一个矩阵。 这是唯一的选择: int** mat = (int**)malloc(rows * sizeof(int*)) for (int index=0;index<row;++index) { mat[index] = (int*)malloc(col * sizeof(int)); }

C – 在函数中分配矩阵

我试图使用一个带有尺寸和三指针的函数来分配矩阵。 我已经分配了一个int **(设置为NULL),我将其地址作为函数的参数传递。 由于某种原因,这给了我一个mem访问冲突。 void allocateMatrix(int ***matrix, int row, int col) { int i; if((*matrix = (int**)malloc(row * sizeof(int*))) == NULL) { perror(“There has been an error”); exit(EXIT_FAILURE); } for(i = 0; i < row; ++i) { if((*matrix[i] = (int*)malloc(col * sizeof(int))) == NULL) { perror("There has been an error"); exit(EXIT_FAILURE); } } } /* […]

使用CUDA减少矩阵行

Windows 7, NVidia GeForce 425M. 我写了一个简单的CUDA代码来计算矩阵的行和。 矩阵具有单维表示(指向浮点的指针)。 代码的串行版本如下(它有2循环,如预期的那样): void serial_rowSum (float* m, float* output, int nrow, int ncol) { float sum; for (int i = 0 ; i < nrow ; i++) { sum = 0; for (int j = 0 ; j < ncol ; j++) sum += m[i*ncol+j]; output[i] = sum; } } […]

如何通过指针处理矩阵中的子矩阵?

我有一个大小为n的矩阵。 举个例子: 我的递归函数对位于矩阵边界的元素进行处理。 现在我想在内部矩阵上调用它(递归调用): 这是我的递归函数的原型: void rotate(int** mat, size_t n); 我知道2D数组是数组中的数组。 我知道*(mat+1) + 1)将给出应该是我的新矩阵的基地址的内存地址。 这是我试过的: rotate((int **)(*(mat+1) + 1), n-2) 但它不起作用,当我尝试使用[][]访问它时,我得到一个段错误。

C中未知矩阵的Dynamica分配

我需要获取用户输入的文件并将其乘以另一个文件。 我知道该怎么做。 问题是一个文件是一个数组,另一个是矩阵。 我需要在矩阵的第一行扫描以找到矩阵的大小,然后我需要从文件中动态分配矩阵和数组。 这是我到目前为止: #include #include #include #include int main() { int row1, col1; //These values need to be pulled from the first file// char filename1[100]; //Setting the file name for entry and setting the limit to 100// FILE* fp1; //FILE must be set as a pointer (FILE must also be capitalized)// printf(“Enter file […]

动态分配矩阵的function

我想创建一个函数来分配(使用malloc / calloc )一个声明为双指针的矩阵。 我理解双指针矩阵如何工作以及如何使用malloc分配它,但是当我传递我的矩阵(在main()声明并初始化为NULL )时,我的程序崩溃了。 我想错误是我的allocMatrix()函数,因为如果我在main分配矩阵,所有工作都顺利进行。 谢谢 :-) 主要: #include #include #include “Data.h” #define ROW 5 #define COL 5 int main(void) { int i,j, ret; int nRow, nCol; int **mat=NULL; // double pointer matrix nRow = 0; nCol = 0; //Insert n of row and columns printf(“Insert n of rows and columns:\n”); scanf(“%d %d”, […]