使用 Arrayfire 设置索引值
Set a index value with Arrayfire
我正在尝试使用自定义值修改现有 Arrayfire 矩阵中的值。下面是将多个行和列更改为指定值 (1.0) 的示例,但是我正在努力不做两件事:
- 改为更改要更改的区域的尺寸 (3x3) -> (2x2)。
- 水平或垂直移动要更改的区域。我所做的任何更改似乎都会出现无效索引错误。
use arrayfire::{constant, Dim4, Seq, assign_seq, print};
let a = constant(2.0 as f32, Dim4::new(&[5, 3, 1, 1]));
let b = constant(1.0 as f32, Dim4::new(&[3, 3, 1, 1]));
let seqs = &[Seq::new(1.0, 3.0, 1.0), Seq::default()];
print(&a);
// 2.0 2.0 2.0
// 2.0 2.0 2.0
// 2.0 2.0 2.0
// 2.0 2.0 2.0
// 2.0 2.0 2.0
let sub = assign_seq(&a, seqs, &b);
print(&sub);
// 2.0 2.0 2.0
// 1.0 1.0 1.0
// 1.0 1.0 1.0
// 1.0 1.0 1.0
// 2.0 2.0 2.0
经过几天的研究,我联系了图书馆的作者后找到了答案:
use arrayfire::{constant, Dim4, Seq, assign_seq, print};
//This is the "parent" matrix.
let a = constant(2.0 as f32, Dim4::new(&[4, 4, 1, 1]));
//To modify a 2x2 area inside a, this must be 2x2 as well
let b = constant(1.0 as f32, Dim4::new(&[2, 2, 1, 1]));
//Use two seq::new objects, NOTE that matrices are column major.
let seqs = &[Seq::new(1.0, 1.0, 1.0), Seq::new(1.0, 1.0, 1.0)];
Arrayfire 3.7.1 还引入了一种新的优雅语法:
let mut a = arrayfire::constant(2.0 as f32, arrayfire::dim4!(4, 4));
let b = arrayfire::constant(1.0 as f32, arrayfire::dim4!(2,2));
arrayfire::print(&a);
arrayfire::eval!(a[1:1:1, 1:1:1] = b);
arrayfire::print(&a);
我正在尝试使用自定义值修改现有 Arrayfire 矩阵中的值。下面是将多个行和列更改为指定值 (1.0) 的示例,但是我正在努力不做两件事:
- 改为更改要更改的区域的尺寸 (3x3) -> (2x2)。
- 水平或垂直移动要更改的区域。我所做的任何更改似乎都会出现无效索引错误。
use arrayfire::{constant, Dim4, Seq, assign_seq, print};
let a = constant(2.0 as f32, Dim4::new(&[5, 3, 1, 1]));
let b = constant(1.0 as f32, Dim4::new(&[3, 3, 1, 1]));
let seqs = &[Seq::new(1.0, 3.0, 1.0), Seq::default()];
print(&a);
// 2.0 2.0 2.0
// 2.0 2.0 2.0
// 2.0 2.0 2.0
// 2.0 2.0 2.0
// 2.0 2.0 2.0
let sub = assign_seq(&a, seqs, &b);
print(&sub);
// 2.0 2.0 2.0
// 1.0 1.0 1.0
// 1.0 1.0 1.0
// 1.0 1.0 1.0
// 2.0 2.0 2.0
经过几天的研究,我联系了图书馆的作者后找到了答案:
use arrayfire::{constant, Dim4, Seq, assign_seq, print};
//This is the "parent" matrix.
let a = constant(2.0 as f32, Dim4::new(&[4, 4, 1, 1]));
//To modify a 2x2 area inside a, this must be 2x2 as well
let b = constant(1.0 as f32, Dim4::new(&[2, 2, 1, 1]));
//Use two seq::new objects, NOTE that matrices are column major.
let seqs = &[Seq::new(1.0, 1.0, 1.0), Seq::new(1.0, 1.0, 1.0)];
Arrayfire 3.7.1 还引入了一种新的优雅语法:
let mut a = arrayfire::constant(2.0 as f32, arrayfire::dim4!(4, 4));
let b = arrayfire::constant(1.0 as f32, arrayfire::dim4!(2,2));
arrayfire::print(&a);
arrayfire::eval!(a[1:1:1, 1:1:1] = b);
arrayfire::print(&a);