Ndarray reverse problem?

there is a mutiple dimension data, like this:

[
  [
    [0, 1, 2, 3, 4],
    [5, 6, 7, 8, 9]
  ],
  [
    [10, 11, 12, 13, 14],
    [15, 16, 17, 18, 19]
  ],
  [
    [20, 21, 22, 23, 24],
    [25, 26, 27, 28, 29]
  ]
]

i want to transform to this:

[ 
   [ 
      [25, 26, 27, 28, 29], 
      [20, 21, 22, 23, 24]
   ],
   [ 
      [15, 16, 17, 18, 19], 
      [10, 11, 12, 13, 14] 
   ], 
   [ 
      [5, 6, 7, 8, 9], 
      [0, 1, 2, 3, 4]
   ] 
]

how to do?

You can use invert_axis() twice on your ndarray.

For example:

use ndarray::prelude::*;

fn main() {
    let d: Vec<i32> = (0..30).collect();
    let mut array = ArrayView::from_shape((3, 2, 5), &d).unwrap();

    println!("{}", array);

    // change the original data
    array.invert_axis(ndarray::Axis(0));
    array.invert_axis(ndarray::Axis(1));

    println!("{}", array);
}
1 Like