简体   繁体   English

我怎样才能转换矩阵 <double> 到Matrix <float> ?

[英]How can I convert matrix<double> to Matrix<float>?

I want to calculate something like: Matrix<float> * Matrix<double> 我想计算类似的东西: Matrix<float> * Matrix<double>

the Matrix<float> has about 6M*3 elements , how can I convert the Matrix<double> to Matrix<float> so that I can get a Matrix<float> result. Matrix<float>有大约6M * 3个元素,如何将Matrix<double>转换为Matrix<float>以便我可以获得Matrix<float>结果。

You can convert your double matrix argument to a float matrix using the Map function: 您可以使用Map函数将双矩阵参数转换为浮点矩阵:

Matrix<double> m1 = Matrix<double>.Build.Random(6000000,3);
Matrix<float> m2 = m1.Map(x => (float)x);

Or alternatively 或者

Matrix<float> m2 = m1.Map(Convert.ToSingle);

Here's how to convert an array of double to an array of float, then you just need to convert your matrix to array and vice versa 这里是如何将double数组转换为float数组,然后你只需要将矩阵转换为数组,反之亦然

public static float[][] Convert(double[][] mtx)
{
    var floatMtx = new float[mtx.Length][];
    for (int i = 0; i < mtx.Length; i++)
    {
        floatMtx[i] = new float[mtx[i].Length];
        for (int j = 0; j < mtx[i].Length; j++)
            floatMtx[i][j] = (float)mtx[i][j];
    }
    return floatMtx;
}
Or:

public static float[][] Convert(double[][] mtx)
{
    return mtx.Select(i => i.Select(j => (float)j).ToArray()).ToArray();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM