如何转换ILArray< object>放入ILArray< double>中? [英] How to convert ILArray<object> into ILArray<double>?

查看:73
本文介绍了如何转换ILArray< object>放入ILArray< double>中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ILNumerics读取和写入matfile.我从数据库中获取一些数据作为object [,],因为它包含数字和标题(字符串).

I m using ILNumerics to read and write from/to matfiles. I get some data from a database as object[,] because it contains numbers and headings (strings).

object[,] loaddata的数据结构是System.StringSystem.Double的混合,前两列和第一行包含字符串,其余部分为双精度.我想将双打写入matfile.尺寸大致为loaddata[9000,500].

The data structure of object[,] loaddata is a mixture of System.String and System.Double, first two columns and first row contains strings rest doubles. I want to write the doubles into the matfile. Dimensions are roughly loaddata[9000,500].

这是我通过系统数组进行的非常麻烦的绕行,甚至无法正常工作:

This is my very cumbersome detour via system arrays that doesnt even work:

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转换为double [,]数组?-我无法完成工作.

with the ToSystemMatrix function from here How to convert ILArray into double[,] array? - which I couldnt make work.

有什么想法可以简化吗?以及如何使该功能正常工作(因为它仍然很有用).

any ideas how to simplify this? and also how to make that function work (as it is usefull anyways).

更新

我做了这项工作,但感觉就像用大锤去做....但是它来了:

I made this work but it feels like going at it with a sledgehammer.... But here it comes:

       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)引发了错误.

I also tried to use ILCell instead but ILCell loaddata = DBQuery(...) and cell(loaddata) threw errors.

有什么想法可以使它更整洁吗?

Any ideas how to make this more neat?

推荐答案

请重新考虑您的设计.为了从DBQuery(....)返回多个信息,您可以使用ILCell或某些其他数据结构,该结构可以存储有关标头和元素的类型化信息(比如说DataTable).只是object [,]似乎太笼统了.

Consider rethinking your design. In order to return multiple information from DBQuery(....) you could either utilize ILCell or some other data structure which is able to store typed information about your headers and elements (let's say DataTable). Just object[,] seems too general.

由于我没有关于您的设置的具体信息,因此我将举一个ILCell示例:

Since I don't have specific information about your setup, I'll give one example for 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类中的单元格.单元格在单独的单元格元素中包含标题和数字数据.然后将其用于从单元中检索信息并将其存储到mat文件中.

This creates and returns a cell in the Helper class. The cell contains the header and numeric data in seperate cell elements. It is than be used to retrieve the information from the cell and stores them into a mat file.

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

http://ilnumerics.net/GeneralRules.html -ILNumerics函数的通用规则

http://ilnumerics.net/GeneralRules.html - general rules for ILNumerics functions

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

http://ilnumerics.net/hdf5-interface.html - more options of writing compatible formats: HDF5

这篇关于如何转换ILArray&lt; object&gt;放入ILArray&lt; double&gt;中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆