在存储为double *d
(column major)的(m x n)数组中,选择行和列范围的最快方式是什么:
double *filter(double *mat, int m, int n, int rows[], int cols[]);
调用为:
double *B;
int rows[]= {1,3,5}; int cols[]={2,4};
B = filter(A, 5, 4, rows, cols);
它将返回由元素(1,2),(1,4),(3,2)组成的3乘2子集。。。
最佳答案
c不提供对它的本机支持,所以您必须找到支持它的库,或者手工定义它。
伪码:
a=length of rows // no built in c functionality for this either,
b=length of cols // use a sentinel, or pass to the function
nmat = allocate (sizeof(double)*a*b) on the heap
for (i=0; i<a; ++i)
for (j=0; j<b; ++j)
// Note the column major storage convention here (bloody FORTRAN!)
nmat[i + j*b] = mat[ rows[i] + cols[j]*n ];
return nmat
// caller responsible for freeing the allocated memeory
我对你面临的两个大问题发表了评论。