我正在尝试访问 Rust 中的代数矩阵的各个元素,但一直出现错误。我也没有在文档中找到任何示例来说明如何访问单个元素——就像我访问一些多维数组一样。
这是我一直在尝试的:
use nalgebra::DMatrix; // 0.21.0
fn main() {
let b = DMatrix::<f64>::zeros(4, 4);
println!("{:?}", b[0][1]);
}
当我编译这段代码时,我得到
error[E0608]: cannot index into a value of type `f64`
--> src/main.rs:5:22
|
5 | println!("{:?}", b[0][1]);
| ^^^^^^^
我不确定如何解释此消息,或者我哪里出错了。
最佳答案
检查 the documentation for Matrix::index
:
pub fn index<'a, I>(&'a self, index: I) -> I::Output
where
I: MatrixIndex<'a, N, R, C, S>,
如果我们看一下 implementers of MatrixIndex
,我们看到许多类型,包括usize
((usize, usize)
) 的元组:
println!("{:?}", b[(0, 1)]);
关于rust - 如何在 Rust 中访问代数矩阵的各个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40901735/