gpt4 book ai didi

multidimensional-array - 使用ndarray实现逐行操作

转载 作者:行者123 更新时间:2023-12-03 11:28:29 25 4
gpt4 key购买 nike

我想执行一个循环操作,该操作将执行以下操作(操作必须逐个元素地进行)。

let mut spec = array![] // matrix mxn
let exponencial = array![] // 1xn

for k in 0..N {
spectrum [k, ..] = spectrum [k, ..] * exponential;
}
如何正确有效地执行此操作?我的意图是将其实现为一个循环,以与其他语言的实现进行比较。我必须使用一些中间临时数组吗?
尝试1
use ndarray::prelude::*; // 0.13.1

fn main() {
// All rows in same time
let mut spec = array![[1, 2, 3, 4], [5, 6, 7, 8]]; // mxn
let exponencial = array![[1, 1, 1, 1]]; // matrix 1xn

let _result = spec.assign(&(&spec * &exponencial));
// or spec = &spec*&exponencial;

// In the form of a loop. one row each iteration.
// This is the implementation that I want to make to be able
// to implement it to be able to compare with other languages
// 1)
let mut spec = array![[1, 2, 3, 4], [5, 6, 7, 8]]; // mxn
let exponencial = array![[1, 1, 1, 1]]; // matrix 1xn
spec.slice_mut(s![0, ..])
.assign(&(&(spec.row(0)) * &exponencial.row(0)));
}
playground
error[E0502]: cannot borrow `spec` as immutable because it is also borrowed as mutable
--> src/main.rs:18:21
|
17 | spec.slice_mut(s![0, ..])
| ---- mutable borrow occurs here
18 | .assign(&(&(spec.row(0)) * &exponencial.row(0)));
| ------ ^^^^ immutable borrow occurs here
| |
| mutable borrow later used by call
尝试2
use ndarray::prelude::*; // 0.13.1

fn main() {
// All rows in same time
let mut spec = array![[1, 2, 3, 4], [5, 6, 7, 8]]; // mxn
let exponencial = array![[1, 1, 1, 1]]; // matrix 1xn

let result = spec.assign(&(&spec * &exponencial));
// or spec = &spec*&exponencial;

// In the form of a loop. one row each iteration.
// This is the implementation that I want to make to be able
// to implement it to be able to compare with other languages
// 2)
let mut spec = array![[1, 2, 3, 4], [5, 6, 7, 8]]; // mxn
let exponencial = array![[1, 1, 1, 1]]; // matrix 1xn
spec.row_mut(0) = &spec.row(0) * &exponencial.row(0);
}
playground
error[E0308]: mismatched types
--> src/main.rs:17:23
|
17 | spec.row_mut(0) = &spec.row(0) * &exponencial.row(0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `ndarray::ViewRepr`, found struct `ndarray::OwnedRepr`
|
= note: expected struct `ndarray::ArrayBase<ndarray::ViewRepr<&mut {integer}>, _>`
found struct `ndarray::ArrayBase<ndarray::OwnedRepr<{integer}>, _>`

error[E0070]: invalid left-hand side of assignment
--> src/main.rs:17:21
|
17 | spec.row_mut(0) = &spec.row(0) * &exponencial.row(0);
| --------------- ^
| |
| cannot assign to this expression

最佳答案

除了bnaecker's answer之外,如果要在循环中实现它,还可以使用 axis_iter_mut() 沿轴进行迭代:

use ndarray::prelude::*;

fn main() {
let mut spec = array![
[1, 2, 3, 4],
[5, 6, 7, 8]
];

let expo = array![2, 2, 2, 2];

for mut row in spec.axis_iter_mut(Axis(0)) {
row *= &expo;
}

println!("{}", spec);
}
[[2, 4, 6, 8],
[10, 12, 14, 16]]

关于multidimensional-array - 使用ndarray实现逐行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64864816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com