1

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
ExtaticGUR
  • 31
  • 4
  • Have you tried examples from tutorial http://arrayfire.org/arrayfire-rust/arrayfire/book/indexing.html ? If you want to use a 2x2 matrix, you have to accordingly change the seqs also - to `Seq::new(1.0, 2.0, 1.0)` Have you done that ? – pradeep Sep 07 '20 at 06:08
  • I have the result is an error: "thread 'main' panicked at 'Error message: Size is incorrect" – ExtaticGUR Sep 08 '20 at 00:28
  • That would mean either the rhs or lhs sizes are not matching. Please double check that. Also, you cannot use two sequences to index a single dimension array, that can also lead to this error. – pradeep Sep 08 '20 at 09:49
  • Thank you for your continued help. I am using a Rust crate that enables Arrayfire so I will need to look at the method signatures to determine how the information is stored (in 1d or Many-d array) – ExtaticGUR Sep 08 '20 at 22:33

1 Answers1

1

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);
ExtaticGUR
  • 31
  • 4