Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
指针,内存分配:矩阵乘法中的行索引(C编程)_C - Fatal编程技术网

指针,内存分配:矩阵乘法中的行索引(C编程)

指针,内存分配:矩阵乘法中的行索引(C编程),c,C,我很难理解问题中的“a行索引”指的是什么。。。你能给我举个例子吗?为什么 sum = sum + a[(row * a_cols) + k] * b[k * b_cols + col]? 谢谢大家! double dotProduct(double a[], const unsigned a_rows, const unsigned a_cols, /* a is a matrix with a_rows rows and a_cols columns */

我很难理解问题中的“a行索引”指的是什么。。。你能给我举个例子吗?为什么

sum = sum + a[(row * a_cols) + k] * b[k * b_cols + col]?
谢谢大家!

double dotProduct(double a[], const unsigned a_rows, const unsigned a_cols,
                  /* a is a matrix with a_rows rows and a_cols columns */
                  double b[], const unsigned b_cols,
                  /* b is also a matrix.  It has a_cols rows and b_cols columns */
                  unsigned row, // an **index of a row of a**
                  unsigned col) // an index of a column of b
{
    int k; // loop variable

    double sum = 0.0; // the result of the dot product is stored here
    k = 0;
    while (k < a_cols) { // recall: a_cols == b_rows
            /* we need to multiply a[row, k] and b[k, col] and add that to sum */
            sum = sum + a[(row * a_cols) + k] * b[k * b_cols + col];
            /* recall a[i,j] is stored at a[(i * a_cols) + j]
                  and b[i,j] is stored at b[(i * b_cols) + j] */
            k += 1;
    }

    return sum;
}
double dotProduct(双a[],常量无符号a_行,常量无符号a_列,
/*a是一个包含a_行和a_列的矩阵*/
双b[],常量无符号b_列,
/*b也是一个矩阵,它有a列和b列*/
无符号行,//一行的**索引**
unsigned col)//b列的索引
{
int k;//循环变量
double sum=0.0;//点积的结果存储在这里
k=0;
而(k
编辑我的错误,忘记我的第一个答案,我误解了这个问题

答复: dotProduct()方法的第5个参数,命名为row,作为a的行的索引
这意味着它是一个0到(a_u行-1)的值,用于指定“矩阵”a的一个特定行。(实际上,它可以是一个1对a的行值。这只是一个惯例问题;数学界的人倾向于喜欢“基于1”的索引,程序员更喜欢基于0的值。请参阅Adam Liss在备注中对此主题的有趣评论)
由于矩阵a是在一维数组中实现的,因此需要使用简单的算法来处理a中按行索引的行上的所有单元格

“公式”是按顺序访问所有这些单元

for (int c = 0; c < a_cols; c++)
{
    A_Cell_Value = a[row * a_cols + c];
}
for(int c=0;c
类似的公式将用于扫描给定b列的单元格。但是,在这种情况下,索引col将用作偏移量,b_cols用作因子。具体来说,

// not an error this is a_cols which also corresponds to the number of rows of
// matrix b, so that these matrices would be compatible for multiplication    
for (int r = 0; r < a_cols; r++) 
{
     B_Cell_Value = b[(r * b_cols) + col];
}
//不是错误这是一个与
//矩阵b,这样这些矩阵就可以进行乘法
for(int r=0;r
我认为以上内容为您提供了通过行或列的单元格进行迭代的必要理解。我让你把这些都放在你的申请表中

一些提示: -执行一些参数值检查。这将避免在这些矩阵上出现越界错误。 -在程序中引入抽象的一个好方法是引入一个函数,该函数从矩阵维度和两行两列索引返回矩阵的单个单元格的值。例如:

// returns the matrix's cell value at RowIdx and colIdx
double GetCell(double matrix[], int nbOfRow, int nbOfColumns,
               int rowIdx, int colIdx)
{
   if (rowIdx < 0 || rowIdx >= nbOfRows ||
       colIdx <0 || colIdx >= nbOfColumns
      )
   {
      printf("bad arguments in GetCell()\n");
      return 0;   // print
    }
    return matrix[rowIdx * nbOfColumns + colIdx];
}
//返回RowIdx和colIdx处矩阵的单元格值
双GetCell(双矩阵[],int-nbOfRow,int-nbOfColumns,
int rowIdx,int colIdx)
{
如果(rowIdx<0 | | rowIdx>=nbOfRows||
colIdx=nbOfColumns
)
{
printf(“GetCell()中的错误参数\n”);
返回0;//打印
}
返回矩阵[rowIdx*nbOfColumns+colIdx];
}
以这种方式,您将抽象(=隐藏有关)这些线性存储的丑陋矩阵的详细信息,并能够在点积公式级别根据行和列索引来解决它们

换句话说,GetCell()函数担心如何根据其对矩阵实现结构的了解找到正确的单元格。它不必知道这个单元格值将用于什么。DotProduct计算逻辑担心A的哪一系列单元与B的哪一系列单元相乘,根据它们的行/列(i/j等)来“自然地”寻址这些单元中的每一个,并且不需要知道数据如何有效地存储在矩阵实现中

编辑我的错,忘记我的第一个答案,我误解了这个问题

答复: dotProduct()方法的第5个参数,命名为row,作为a的行的索引
这意味着它是一个0到(a_u行-1)的值,用于指定“矩阵”a的一个特定行。(实际上,它可以是一个1对a的行值。这只是一个惯例问题;数学界的人倾向于喜欢“基于1”的索引,程序员更喜欢基于0的值。请参阅Adam Liss在备注中对此主题的有趣评论)
由于矩阵a是在一维数组中实现的,因此需要使用简单的算法来处理a中按行索引的行上的所有单元格

“公式”是按顺序访问所有这些单元

for (int c = 0; c < a_cols; c++)
{
    A_Cell_Value = a[row * a_cols + c];
}
for(int c=0;c
类似的公式将用于扫描给定b列的单元格。但是,在这种情况下,索引col将用作偏移量,b_cols用作因子。具体来说,

// not an error this is a_cols which also corresponds to the number of rows of
// matrix b, so that these matrices would be compatible for multiplication    
for (int r = 0; r < a_cols; r++) 
{
     B_Cell_Value = b[(r * b_cols) + col];
}
//不是错误这是一个与
//矩阵b,这样这些矩阵就可以进行乘法
for(int r=0;r
我认为以上内容为您提供了通过行或列的单元格进行迭代的必要理解。我让你把这些都放在你的申请表中

一些提示: -执行一些参数值检查。这将避免在这些矩阵上出现越界错误。 -在程序中引入抽象的一个好方法是引入一个返回单个单元格值的函数
 0,  1,  2,  3,  4,  5,
 6,  7,  8,  9, 10, 11,
12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23,
 0,  1,  2,  3,
 4,  5,  6,  7,
 8,  9, 10, 11,
12, 13, 14, 15,
16, 17, 18, 19,
20, 21, 22, 23,
 0,  1,  2,
 3,  4,  5,
 6,  7,  8,
 9, 10, 11,
12, 13, 14,
15, 16, 17,
18, 19, 20,
21, 22, 23,
 0,  1,
 2,  3,
 4,  5,
 6,  7,
 8,  9,
10, 11,
12, 13,
14, 15,
16, 17,
18, 19,
20, 21,
22, 23,