diagonal

Function diagonal 

Source
pub fn diagonal<T>(
    array: ArrayView<'_, T, Ix2>,
    k: isize,
) -> Result<Array<T, Ix1>, &'static str>
where T: Clone + Zero,
Expand description

Extract a diagonal from a 2D array

§Arguments

  • array - The input 2D array
  • k - Diagonal offset (0 for main diagonal, positive for above, negative for below)

§Returns

A 1D array containing the specified diagonal

§Examples

use ndarray::array;
use scirs2_core::ndarray_ext::matrix::diagonal;

let a = array![[1, 2, 3], [4, 5, 6], [7, 8, 9]];

// Extract main diagonal
let main_diag = diagonal(a.view(), 0).unwrap();
assert_eq!(main_diag, array![1, 5, 9]);

// Extract superdiagonal
let super_diag = diagonal(a.view(), 1).unwrap();
assert_eq!(super_diag, array![2, 6]);

// Extract subdiagonal
let sub_diag = diagonal(a.view(), -1).unwrap();
assert_eq!(sub_diag, array![4, 8]);