我需要乘以矩阵,我使用以下代码:
- (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中构建矩阵代码,但它不起作用。
我如何正确构建它?
答案 0 :(得分: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)