-2

我需要将使用以下代码的矩阵相乘:

- (NSNumber *)matrixsender
{
    double matrix[4][2] = {};
    double vectorsend[4] = {};
    [self matrix_multiply:matrix[4][2]:vectorsend[4]];
}


-(NSNumber *)matrix_multiply:(double) mat:(double)vector
{
    double vector_result[] = vector.Length;
    for (int i = 0; i < vector.Length; i++) {
        for (int j = 0; j < mat.GetLength(1); j++) {
            vector_result[i] += mat[i][j]*vector[j];
        }
    }

    return [NSNumber numberWithDouble:vector_result[]];
}

实际上,它来自 c# 代码,但我无法将其转换为 Objective-C,然后我在 Objective-C 中构建矩阵代码,但它不起作用。

我怎样才能正确构建它?

4

1 回答 1

0
double **matrix_mult(double *a, double *b, int nr1, int nc1, int nc2) {
  double **c;
  int i, j, k;
  c = malloc(sizeof(double *) * nr1);
  for (i = 0; i < nr1; i++) {
    c[i] = malloc(sizeof(double) * nc2);
    for (k = 0; k < nc2; k++){
        c[i][k] = 0.0;
        for (j = 0; j < nc1; j++) c[i][k] += (a[i][j]) * (b[j][k]);
    }
  }
  return c;
}

double *c = matrix_mult(a,b,4,4,4)
于 2013-02-09T10:36:35.923 回答