Pergunta

I need to add a new element (double value) into an existing DenseVector. I found some workarounds, however, what is the right way how to do this in Math.NET?

Foi útil?

Solução

There is currently no "intended" way to do it. The Matrix type does have InsertRow/InsertColumn methods, and there has been some demand to add an Insert method also for the Vector type. I've just opened a new ticket #159 to track it.

In the meantime, you could use the following routine (I'm using v3.0.0-alpha5 here):

Vector<T> InsertAt<T>(Vector<T> v, int i, T value) where T : struct, IEquatable<T>, IFormattable
{
    var res = Vector<T>.Build.Dense(v.Count+1);
    if (i > 0) v.Storage.CopySubVectorTo(res.Storage, 0, 0, i, true);
    if (i < v.Count) v.Storage.CopySubVectorTo(res.Storage, i, i+1, v.Count-i, true);
    res.At(i, value);
    return res;
}

var v = Vector<double>.Build.Dense(5, i => i);
var vx = InsertAt(v, 5, 100);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top