我正在使用n代数,我想通过将一个矩阵设置为另一个具有兼容维度的列来修改它,如下所示:

    let zero: T = convert(0.0);
    let mut basis = DMatrix::from_element(rows, rank, zero);
    // matrix is an input with dimensions rows x cols
    for i in 0..location.len()
    {
        basis.column_mut(i) = matrix.column(location[i]);
    }

我还try 了取消对任务双方的引用,并寻找了某种"分配"方法,但没有成功.

set_column不起作用,因为DMatrix不实现DimName

我目前的工作是这样的,但我一点也不喜欢:

 // Construct basis vectors initialized to 0.
    let zero: T = convert(0.0);
    let mut basis = DMatrix::from_element(rows, rank, zero);

    for i in 0..location.len()
    {
        // Copy the pivot column from the matrix.
        let col = location[i];
        for r in 0..matrix.nrows()
        {
            basis[(r, i)] = matrix[(r, col)];
        }
    }

推荐答案

我也不知道column_mut是什么意思,它做什么,或者它应该如何运作.关于它的文档相当稀少(不存在).我认为set_column做了你想做的事情(也许你叫错了?):

use nalgebra::DMatrix;
let rows = 2;
let cols = 3;
let zero = 0.0;
let mut basis = DMatrix::from_element(rows, cols, zero);
let matrix = DMatrix::from_row_slice(rows, cols, &[
  1.0, 3.0, 5.0,
  2.0, 4.0, 6.0
]);
let location = [1, 0, 2];
for i in 0..location.len() {
  basis.set_column(i, &matrix.column(location[i]));
}

(我将您的代码调整为MWE)

Rust相关问答推荐

如何在Rust中获得不可辩驳的'if let'模式警告Mutex锁定?""

无法在线程之间安全地发送future (&Q;)&错误

铁 rust 干线无法使用PowerShell获取环境变量

MutexGuard中的过滤载体不需要克隆

为潜在的下游实现使用泛型绑定而不是没有泛型绑定的trait

制作一片连续整数的惯用Rust 方法?

如何提高自定义迭代器的`extend`性能

有没有一种惯用的方法来判断VEC中是否存在变体?

Rust面向对象设计模式

使用 Option 来分配?

Rust 中的生命周期:borrow 的 mut 数据

按下 Ctrl + C 时优雅地停止命令并退出进程

如何在 Rust 的 Hyper 异步闭包中从外部范围正确读取字符串值

为什么 File::read_to_end 缓冲区容量越大越慢?

如何在 use_effect_with_deps 中设置监听器内的状态?

Rust 中 `Option` 的内存开销不是常量

将一片字节复制到一个大小不匹配的数组中

相交着色器从 SSBO 中读取零

在 Rust 中枚举字符串的最佳方式? (字符()与 as_bytes())

当引用不再被borrow 时,Rust 不会得到它