【发布时间】:2011-09-20 11:47:54
【问题描述】:
我需要在 C 中使用 Matlab find 函数 (reference) 来处理数组:
ind = find(X) 定位数组 X 的所有非零元素,并返回 向量 ind 中这些元素的线性索引。如果 X 是行向量, 那么 ind 是一个行向量;否则, ind 是列向量。如果 X 不包含非零元素或为空数组,则 ind 是 空数组。
一个简单的未经测试的例子:
#include <stdlib.h>
#include <time.h>
int main()
{
/* Initialize variables */
int x[25];
int y[25];
/* initialize random seed: */
srand ( time(NULL) );
/* Fill arrays with stuff */
for (i=0;i<25;i++)
{
x[i] = rand();
y[i] = rand();
}
/* Find */
ind = find((x-y) > 0); // Need to figure out how to do this
}
}
现在问题是由于项目限制,我不能使用像 vector 这样的 Boost 或 C++ 容器。
【问题讨论】:
-
我猜你得自己动手了。
-
@Dima,这是我最初的猜测,希望 Stack Exchange 能证明我错了。
-
@Dorgan,这就是为什么它是微不足道的,也是我快速编写未经测试的东西的原因:p
-
只是一个跛脚的聪明人——别介意我在这里。我会继续删除我的评论...
-
这只是一个简单的
for循环,你可以写100次,比来这里寻找它花费的时间更少! :))