block_diag

Function block_diag 

Source
pub fn block_diag<T>(arrays: &[ArrayView<'_, T, Ix2>]) -> Array<T, Ix2>
where T: Clone + Zero,
Expand description

Create a block diagonal matrix from a sequence of 2D arrays

§Arguments

  • arrays - A slice of 2D arrays to form the blocks on the diagonal

§Returns

A block diagonal matrix with the input arrays on the diagonal

§Examples

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

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

let result = block_diag(&[a.view(), b.view()]);
assert_eq!(result.shape(), &[4, 4]);
assert_eq!(result, array![
    [1, 2, 0, 0],
    [3, 4, 0, 0],
    [0, 0, 5, 6],
    [0, 0, 7, 8]
]);