简体   繁体   中英

MWarray convert double[] to ushort[]

public static double[] ParseDoubleArray(MWArray array)
{
    var vector2d = (array as MWNumericArray).ToArray() as double[,];
    var vector1d = new double[vector2d.Length];
    System.Buffer.BlockCopy(vector2d, 0, vector1d, 0, vector2d.Length * sizeof(double));
    return vector1d;
}

this is my function for getting the double[] from MWArray however why i do this:

prepImage.RawData = Array.ConvertAll(prepRawData, Convert.ToUInt16);

I sometimes get an exception because matlab is returning doubles too big for conversion.

has anyone came across this issue? i can crop the numbers but is there another solution?

UInt16 , as its name implies, holds unsigned 16 bit integers (values from 0 to 65535). On the other hand, the double structure ranges from -1.79769313486232e308 to 1.79769313486232e308.

The issue here is that your Matlab code returns either a negative value, or a positive value greater than 65535. Matlab will also assign NaN to any uninitialized value which is also invalid for UInt16 .

To fix your problem, either make sure that your Matlab code is really only returning values in the 0 to 65535 range or change the data structure on the C# side to something else than UInt16 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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