Set a index value with Arrayfire

230 Views Asked by At

I am trying to modify a value in an existing Arrayfire Matrix with a custom value. Below is an example of changing several rows and columns to a specified value (1.0) However I am struggling no doing two things:

  1. Change the dimensions of the area to be changed (3x3) -> (2x2) instead.
  2. Move the area to be changed Horizontally or vertically. Any change I make seems to give an invalid index error.
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
1

There are 1 best solutions below

0
On BEST ANSWER

After days of research I found an answer after reaching out to the author of the library:

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 also introduces a new elegant syntax:

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);