Skip to content

Non-continguous matrix slices #188

Closed
@kernelmachine

Description

@kernelmachine

@bluss we discussed this on the IRC, but thought I'd record the desired functionality here.

For a project I'm working on, I've been in need of non-contiguous row/column slices of ndarray matrices.

So far, I've been performing the operation via a copy -- stacking row/column views. Note that because stack requires each to-be-stacked array to be 2-dimensional, I use into_shape on each row view in the closure.

/// Rectangular matrix
pub type Mat<A> = OwnedArray<A, (Ix, Ix)>;

pub fn noncontig_2d_row_slice(mat : &Mat<f64>, indices : &Vec<usize>)  -> Mat<f64> {
    let mat = indices.iter()
                        .map(|&x| mat.row(x).into_shape((1,mat.shape()[1]))
                        .ok().expect("indexing Error")).collect::<Vec<_>>();
    stack(Axis(0), mat.as_slice()).ok().expect("stacking error")
}

For example:

fn main(){
    let x = arr2(&[[0.0, 1.0], [1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0, 1.0],[0.0, 1.0]]);
    let s = noncontig_2d_slice(&x,&vec![1,3,5]);
    let target = arr2(&[[1.0,0.0],[1.0,0.0],[0.0, 1.0]]);
    assert!(s.all_close(&target,1e-8))
}

Is this functionality worth integrating into the codebase?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions