# Assignment to 2d array

I get the error 'ndarray: could not broadcast array from shape: [1] to: ' from this function:

``````fn make_products_six(vec: &[usize]) -> Array2<usize> {
println!("5555 = {:?}", 5555);
let this = vec.iter().map(|x| if *x == 666 {1_usize} else {*x}).collect::<Vec<_>>();
let mut results = Array2::<usize>::zeros((this[0]*this[1]*this[2]*this[3]*this[4]*this[5],6));
let mut count: usize = 0;
for index in 0..vec.len() {
println!("(index) = {:?}", (index));
while count < results.shape()[0] {
if vec[index] != 666 {
for i in 0..vec[index] {
results.slice_mut(s![count,index]).assign(&array![i]);
count += 1;
}
}
if vec[index] == 666 {
for i in vec![vec[index]].iter() {
results.slice_mut(s![count,index]).assign(&array![*i]);
count += 1;
}
}
println!("(count,index) = {:?}", (count,index));
}
}
println!("make_products_six results = {:?}",results);
results
}
``````

You need to use the fill function on the slice_mut array, rather than assign

I'm not sure what the error message is and what the involved array dimensions are. Count is an integer and index is an integer, so it's being sliced down into a zero-dimensional array view. That part should work, but maybe it's an inefficient way to do it.

As usual, I'd avoid using indexes in the inner loops of your algorithms.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.