gpt4 book ai didi

c - 如何通过选择 c 中的行和列来查找二维数组的子集?

转载 作者:太空宇宙 更新时间:2023-11-04 03:08:31 25 4
gpt4 key购买 nike

在存储为 double *d(主要列)的(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);

预期返回 A 的 3×2 子集,其中包含元素 (1,2)、(1,4)、(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

我已经评论了你面临的两个大问题。

关于c - 如何通过选择 c 中的行和列来查找二维数组的子集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1281162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com