Skip to content

How to index an entire 1d array from a 2d array? #1214

Answered by jturner314
TrevorSatori asked this question in Q&A
Discussion options

You must be logged in to vote

I'd suggest reading the Slicing and Subviews docs. The ndarray for NumPy users guide may also be of interest.

Here's some sample code for your specific question:

use ndarray::prelude::*;
use std::ops::AddAssign;

fn main() {
    let mut x: Array2<i32> = Array::zeros((10, 5));
    
    // The examples below use `.row_mut()`. Other options include
    // `.slice_mut()` and `.index_axis_mut()`.
    
    // Assign a row.
    x.row_mut(0).assign(&aview1(&[0,0,0,0,0]));
    
    // Fill all elements of a row with the same value.
    x.row_mut(0).fill(0);
    
    // Add to a row in-place (+= operation). Note the `use std::ops::AddAssign` above.
    x.row_mut(0).add_assign(&aview1(&[0,0,0,0,0]));
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@TrevorSatori
Comment options

Answer selected by TrevorSatori
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants