Skip to content

How to perform in-place operations on parts of a vector provided an index array/vector? #419

@rcarson3

Description

@rcarson3

First off, I just want to say that I'm still very much getting the hang of ndarray. So far, I understand the basics like how to perform matrix multiplication, slicing, and in place mapping functions. However, I've found the documentation to be at times a little hard to understand.

Now on to my actual question, so in Numpy or Fortran I can easily do something like the following:

vec = np.zeros((6,1))

index = [1, 3, 5]

mat = np.ones((3,3))
x = np.ones((3,1))

vec[index] = mat.dot(vec)

It's the being able to assign values to a vector or an array at these indices that I'm trying to get a better hold of using ndarray.

Currently, the only way I can really think of how to do this is the following:

let mut vec = Array::<f32,_>::zeros((3,3));

let mut mat = Array::<f32, _>::zeros((3,3));
mat.fill(1.0);

let mut x = Array::<f32, _>::zeros((3,1));
x.fill(1.0);

ind = vec!(1, 3, 5);

let temp = mat.dot(&x);

for (i, &index) = ind.iter().enumerate(){
      vec[[index, 0]] = temp[[i, 0]];
}

I could make this into function, but I'd like to believe there's a built in way to do this in ndarray that I'm just missing or have over looked that avoids the need for the temporary arrays/vectors creation.

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