-->

如何转换ILArray 到ILArray ?(How to convert ILArray

2019-10-21 00:56发布

我米使用ILNumerics读取和/至matfiles写。 我从一个数据库对象获得一些数据[,],因为它包含数字和标题(字符串)。

的数据结构object[,] loaddata是的混合物System.StringSystem.Double前两列,并且第一行包含字符串休息双打。 我想写双打进入matfile。 尺寸大致loaddata[9000,500]

这是我通过这并不甚至工作系统阵列,非常麻烦绕行:

ILArray<object> loaddata = DBQuery(....);
//this next line has error
var tempoobj = ToSystemMatrix<object>(loaddata["2:end", "1:end"]);
double[,] tempdouble = new double[500, 8784];
Array.Copy(tempoobj, tempdouble, tempoobj.Length);
ILArray<double> B = tempdouble;
using (ILMatFile matW = new ILMatFile())
{
      B.Name = "power";
      matW.AddArray(B);
      matW.Write(@"C:\Temp\Test.mat");
}

从这里ToSystemMatrix功能如何ILArray转换为双[,]数组? - 这是我不可能做的工作。

任何想法如何简化呢? 以及如何让该功能的工作(因为它是有用的反正)。

UPDATE

我做了这个工作,但感觉像它用大锤去....但在这里说到:

       object[,] loaddata = DBQuery(...);

        for (int i = 0; i < loaddata.GetLength(0); i++)
        {
            loaddata[i, 0] = Convert.ToDouble(0);
            loaddata[i, 1] = Convert.ToDouble(0);
        }
        for (int i = 0; i < loaddata.GetLength(1); i++)
        {
            loaddata[0, i] = Convert.ToDouble(0);
        }

        double[,] tempdouble = new double[loaddata.GetLength(0), loaddata.GetLength(1)];
        Array.Copy(loaddata, tempdouble, loaddata.Length);
        ILArray<double> B = tempdouble;


        using (ILMatFile matW = new ILMatFile())
        {
            B.Name = "power";
            matW.AddArray(B["1:end","2:end"].T);
            matW.Write(@"C:\Temp\Test.mat");
        }

我还试图用ILCell代替,但ILCell loaddata = DBQuery(...)cell(loaddata)抛出的错误。

任何想法如何使这更整洁?

Answer 1:

考虑重新考虑你的设计。 为了从返回多个信息DBQuery(....)你既可以利用ILCell或其他一些数据结构,它能够存储你的头和元素的类型化信息(假设的DataTable)。 只是对象[,]似乎过于笼统。

由于我没有关于您的设置的具体信息,我会给出一个例子ILCell

using ILNumerics;
using System;

namespace Cell_DBQuery_Example {
    class Program {
        static void Main(string[] args) {

            using (ILScope.Enter()) {
                ILCell data = Helper.DBQuery(); 
                // data is: 
                //Cell [2,2]
                //[0]: <String>           header 1  <Double> [100,200]           
                //[1]: <String>           header 2  <Single> [2,3000]  

                // store into mat file
                ILMatFile mat = new ILMatFile();
                var key1 = (string)data.GetArray<string>(0, 0); 
                mat.AddArray(data.GetArray<double>(0, 1), key1);  // stores rand(100,200) as key: "header1"
                // proceed with other columns...

                // write mat file
                mat.Write("filename"); 
            }
        }

        class Helper : ILMath {

            public static ILRetCell DBQuery() {
                using (ILScope.Enter()) {
                    // prepare return cell
                    ILCell ret = cell(size(2, 2)); 
                    // todo: fetch data from db and replace below example data
                    ret[0, 0] = "header_1";
                    ret[1, 0] = "header_2";
                    ret[0, 1] = rand(100, 200);
                    ret[1, 1] = ones<float>(2, 3000);
                    return ret; 
                }
            }
        }
    }
}

这将创建并返回在细胞Helper类。 单元格包含在单独的电池元件的首部和数字数据。 它是不是被用来检索从细胞,并将它们存储的信息为毡文件。

一些参考: http://ilnumerics.net/Cells.html

http://ilnumerics.net/GeneralRules.html -为ILNumerics功能的一般规则

http://ilnumerics.net/hdf5-interface.html -写兼容格式更多的选择:HDF5



Answer 2:

改变这一行:

 var tempoobj = ToSystemMatrix<object>(loaddata["2:end", "1:end"]);

 var tempoobj = ToSystemMatrix<double>(loaddata["2:end", "1:end"]);

这是假设您选择,所有的数据都是双打loaddata的只是一部分。



文章来源: How to convert ILArray into ILArray?
标签: c# ilnumerics