【问题标题】:Evaluating a matlab function in C# that requires a matrix input在 C# 中评估需要矩阵输入的 matlab 函数
【发布时间】:2015-11-06 17:43:29
【问题描述】:

我为我在 matlab 中创建的神经网络创建了一个函数。我可以在 c# 中成功加载函数,但我不知道如何输入矩阵作为输入,因为数组不起作用。

Matlab 原函数

function energy = simCW(x)
%x is a 4xQ matrix (I always sim 4x1 though)
S = load('CW100', 'CB_CW100');
CB_CW100 = S.CB_CW100;
energy = sim(CB_CW100,x')'; 
end

用于 C# 生成函数的 Matlab 行

genFunction(CB_CW100, 'cbcw.m', 'MatrixOnly','yes', 'ShowLinks','no')

生成函数

function [y1] = cbcw(x1)
%CBCW neural network simulation function.
% [y1] = cbcw(x1) takes these arguments:
%   x = 4xQ matrix, input #1
% and returns:
%   y = 1xQ matrix, output #1
% where Q is the number of samples.

C# 代码 - Source

    static void Main(string[] args)
    {
        MLApp.MLApp matlab = new MLApp.MLApp();
        double[] current = new double[4];
        current[0]= 71;
        current[1] = 74;
        current[2] = 8;
        current[3] = 105;
        matlab.Execute("cbcw");
        object result = null;
        matlab.Feval("cbcw", 1, out result, current);
        object[] res = result as object[];
        Console.WriteLine(res[0]);
        Console.ReadLine();

    }

正在运行的程序是打开一个 matlab 命令行并将以下内容写入控制台:

System.Double[,]

我想在不打开 matlab 的情况下将数字写入控制台,因为这最终将成为没有 matlab 的计算机的独立进程。

【问题讨论】:

    标签: c# matlab matrix


    【解决方案1】:

    尝试将矩阵的每个元素视为输入,在 matlab 函数中,您可以组织这些输入参数来构造矩阵。

    例如

    matlab:

    function [y1] = cbcw(x1,x2,x3,x4)
    x=[x1 X2;x3 x4];
    

    c#:

    static void Main(string[] args)
        {
            MLApp.MLApp matlab = new MLApp.MLApp();
            double[] current = new double[4];
            current[0]= 71;
            current[1] = 74;
            current[2] = 8;
            current[3] = 105;
            matlab.Execute("cbcw");
            object result = null;
            matlab.Feval("cbcw", 1, out result, current[0],current[1],current[2],current[3]);
            object[] res = result as object[];
            Console.WriteLine(res[0]);
            Console.ReadLine();
    
        }
    

    【讨论】:

      猜你喜欢
      • 2013-08-30
      • 1970-01-01
      • 2021-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-10
      • 2011-02-25
      • 1970-01-01
      相关资源
      最近更新 更多