计算c#中需要矩阵输入的matlab函数

本文关键字:输入 matlab 函数 计算 | 更新日期: 2023-09-27 18:07:43

我为我在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

Matlab中的c#生成函数行

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#代码源代码

    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函数

尝试将矩阵的每个元素视为输入,并且在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();
    }