You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi! In the example below, function load2 works while in load the compiler complains:
error[E0594]: cannot assign to data in an index of `ArrayBase<S, Dim<[usize; 2]>>`
--> src/lib.rs:25:13
|
25 | arr[[idx1, idx2]] = *elem;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ cannot assign
|
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `ArrayBase<S, Dim<[usize; 2]>>`
What would be an efficient and correct way to fill the array in load's case?
use ndarray::{ArrayBase, Ix2, Data, DataOwned, OwnedRepr}; // 0.15.6
use num_traits::Float; // 0.2.15
struct TestStruct<T: Float, S: Data<Elem = T>> {
data: ArrayBase<S, Ix2>,
}
impl<T, S> TestStruct<T, S>
where
T: Float,
S: Data<Elem = T>,
{
fn load(v: Vec<T>) -> TestStruct<T, S>
where
S: DataOwned,
{
let nrows = 3;
let ncols = 100;
let mut arr = ArrayBase::<S, Ix2>::zeros((nrows, ncols));
let mut idx1;
let mut idx2 = 0;
for (n, elem) in v.iter().enumerate() {
idx1 = n % 3;
arr[[idx1, idx2]] = *elem;
if idx1 == nrows-1 {
idx2 += 1;
}
}
TestStruct { data: arr }
}
}
impl<T> TestStruct<T, OwnedRepr<T>>
where
T: Float,
{
fn load2(v: Vec<T>) -> TestStruct<T, OwnedRepr<T>>
{
let nrows = 3;
let ncols = 100;
let mut arr = ArrayBase::<OwnedRepr<T>, Ix2>::zeros((nrows, ncols));
let mut idx1;
let mut idx2 = 0;
for (n, elem) in v.iter().enumerate() {
idx1 = n % 3;
arr[[idx1, idx2]] = *elem;
if idx1 == nrows-1 {
idx2 += 1;
}
}
TestStruct { data: arr }
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi! In the example below, function
load2
works while inload
the compiler complains:What would be an efficient and correct way to fill the array in
load
's case?Beta Was this translation helpful? Give feedback.
All reactions