- Code: Select all
for i = 1 : N
x(i) = y(find(rand <= w,1));
end
Assume that x, y and w are the same lengths e.g. N = 1e6 and that w in the range of 0 to 1 and is monotonically non-decreasing.
Any help would be a great help.
Thanks in advance!
Moderator: pavanky
for i = 1 : N
x(i) = y(find(rand <= w,1));
end
for (int i = 0; i < N; i++) {
array rnd = randu(1, 1);
array cmp = diff((rnd <= w).as(f32));
array val, array idx;
max(val, idx, cmp);
x(i) = y(idx + 1);
}
array get_idx(array w, int n)
{
int N = w.elements();
array wn = tile(w, 1, n);
array rnd = tile(randu(1, n), N, 1);
array cmp = diff1((rnd <= wn).as(f32));
array val, idx;
max(val, idx, cmp);
return idx + 1;
}
int main()
{
/// other code
for (int i = 0; i < N - 1000; i += 1000) {
array idx = get_idx(w, 1000);
x(i + seq(1000)) = y(idx);
}
int n = N % 1000;
int off = N - n;
array idx = get_idx(w, n);
x(off + seq(n)) = y(idx);
/// other code
}
int lower_bound(array &in, double val)
{
array tmp = diff1((val <= in ).as(f32));
float res;
int idx;
max<float>(&res, &idx, tmp);
return idx + 1;
}
int upper_bound(array &in, double val)
{
array tmp = diff1((val < in).as(f32));
float res;
int idx;
max<float>(&res, &idx, tmp);
return idx + 1;
}
int main(int argc, char **argv)
{
try {
array a = round(10 * sort(randu(20, 1)));
print(a);
printf("lower: %d, upper: %d\n", lower_bound(a, 4), upper_bound(a, 4));
} catch (af::exception& e) {
fprintf(stderr, "%s\n", e.what());
throw;
}
return 0;
}
a [20 1] =
0.0000
1.0000
2.0000
3.0000
3.0000
3.0000
4.0000
4.0000
4.0000
5.0000
5.0000
7.0000
7.0000
7.0000
7.0000
8.0000
8.0000
9.0000
9.0000
10.0000
lower: 6, upper: 9
Return to [archive-commercial] Programming & Development with ArrayFire