-
Notifications
You must be signed in to change notification settings - Fork 323
Array3 from Vec<Array2<Complex<f32>>> #607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
It's not possible to create an For your use-case, if you know the shape of extern crate ndarray;
use ndarray::prelude::*;
fn main() {
let iter_len = 5;
let rank = 4;
let mut array_3d = Array3::zeros((iter_len, rank, rank));
for (i, mut v) in array_3d.axis_iter_mut(Axis(0)).enumerate() {
let complex_vec = vec![i; rank * rank];
let mat = Array::from_shape_vec((rank, rank), complex_vec).unwrap();
// or: let mat = ArrayView2::from_shape((rank, rank), &complex_vec).unwrap();
v.assign(&mat);
}
println!("{}", array_3d);
} If you don't know the shape ahead-of-time, then I'd suggest placing the elements (flattened) in a single extern crate ndarray;
use ndarray::prelude::*;
fn main() {
let rank = 4;
let mut data = Vec::new();
let mut iter_len = 0;
for i in 0..5 {
iter_len += 1;
let complex_vec = vec![i; rank * rank];
data.extend_from_slice(&complex_vec);
}
let array_3d = Array3::from_shape_vec((iter_len, rank, rank), data).unwrap();
println!("{}", array_3d);
} If you want to stick with your existing implementation, you could do this: extern crate ndarray;
use ndarray::prelude::*;
fn main() {
let rank = 4;
let mut matrix_data = vec![];
let mut iter_len = 0;
for i in 0..5 {
iter_len += 1;
let complex_vec = vec![i; rank * rank];
let mat = Array2::from_shape_vec((rank, rank), complex_vec).unwrap();
matrix_data.push(mat);
}
let data = matrix_data.iter().flatten().cloned().collect();
let array_3d = Array3::from_shape_vec((iter_len, rank, rank), data).unwrap();
println!("{}", array_3d);
} |
Fantastic! Thank you. The first option seems like it will work perfect. More likely, I'll take the time to understand why this method might not be the best way to handle creating the array and will end up refactoring! |
Is it not possible to create a 3D array from a vec of Array2? Mine are complex, but I'm not sure this matters. Here's what I'm trying:
and the error I'm getting is
I saw shape_from_vec, but that doesn't work for me either. I think because of the shape of my Vec
The text was updated successfully, but these errors were encountered: