1

I need help making sense of Rust's ndarray remove_index() function. For example, I have a 3x3 two dimensional array:

use ndarray::{arr2};

let mut arr = arr2([1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9]);

I would like to remove the element with a value 5 at position [1,1]. How do I do that with the remove_index() function? I don't understand, how do I specify the axes and indices of a 2D array.

arr.remove_index(...);
mabalenk
  • 887
  • 1
  • 8
  • 17
  • 5
    I don't think you _can_ remove a single element like this. As I understand it, the length of the subarrays must all remain the same, so removing 5 would also remove 2 and 8 or 4 and 6 -- you would be removing that entire column or row. If you want a type where each subarray can have a different length, I think you just want a `Vec>`. – cdhowie Feb 20 '23 at 19:56

1 Answers1

0

You could swap the item to be deleted to the edge:

arr.swap([1, 1], [1, 2]); // [1, 2, 3],
                          // [4, 6, 5],
                          // [7, 8, 9]

arr.swap([1, 1], [2, 1]); // [1, 2, 3],
                          // [4, 8, 6],
                          // [7, 5, 9]
Kaplan
  • 2,572
  • 13
  • 14