c# - 使用 C# 包装器时出现 Matlab 异常

标签 c# .net matlab matlab-compiler

我制作了 clustering algorithm implemented in matlab 的 dll 版本

此外,我还下载了 working sample of the input data (我正在使用玩具问题数据)并且我正在阅读它并将其转换为 Matlab 已知的数据类型。

但是,运行算法时出现以下错误:

... MWMCR::EvaluateFunction error ... Dimensions of matrices being concatenated are not consistent. Error in => apclusterSparse.m at line 178.

这是我的代码:(抱歉?)

public static double[,] ReadSimilarities()
    {
        string line;
        string[] splittedLine;
        System.IO.StreamReader file = new System.IO.StreamReader("C:\\Code\\FCT\\Thesis\\similarities.txt");

        List<List<string>> values = new List<List<string>>();

        List<string> lineValues;

        while ((line = file.ReadLine()) != null)
        {
            splittedLine = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
            lineValues = new List<string>(splittedLine.Count());

            for (int i = 0; i < splittedLine.Count(); i++)
            {
                lineValues.Add(splittedLine[i]);
            }

            values.Add(lineValues);
        }

        file.Close();

        double[,] result = new double[values.Count, 3];

        for (int i = 0; i < values.Count; i++)
        {
            result[i, 0] = Convert.ToDouble(values.ElementAt(i).ElementAt(0));
            result[i, 1] = Convert.ToDouble(values.ElementAt(i).ElementAt(1));
            result[i, 2] = Convert.ToDouble(values.ElementAt(i).ElementAt(2));
        }
        return result;
    }

    public static double[] ReadPreferences()
    {
        string line;
        System.IO.StreamReader file = new System.IO.StreamReader("C:\\Code\\FCT\\Thesis\\preferences.txt");
        List<string> values = new List<string>();

        while ((line = file.ReadLine()) != null)
        {
            values.Add(line);
        }

        double[] result = new double[values.Count];
        for (int i = 0; i < values.Count; i++)
        {
            result[i] = Convert.ToDouble(values.ElementAt(i));
        }

        return result;
    }


    public ActionResult Index()
    {
        ApClusterSparse apClusterSparse = new ApClusterSparse();

        double[,] similarities = ReadSimilarities();
        double[] preferences = ReadPreferences();

        MWNumericArray matLabSimiliaritiesArray = new MWNumericArray(similarities);
        MWNumericArray matLabPreferencesArray = new MWNumericArray(preferences);

        MWArray argsOut;

        try
        {
            argsOut = apClusterSparse.apclusterSparse(matLabSimiliaritiesArray, matLabPreferencesArray);
        }
        catch (Exception e)
        {

        }

        return View();
    }

谢谢。

最佳答案

快速查看代码的第 178 行,您似乎正在将 Nx2 数组与 p 连接起来。 ,然后将其与 s 连接起来,有时 N 定义为 length(p) ,有时为 size(s,1)有时称为 tmp .

我不打算对此进行调试,但我建议您应该修改代码,以便在第 178 行之前显示或以某种方式输出 N 的值。 , p , stmp 。这将使您了解为什么它们不能连接 - 我猜测它们具有不同的维度。

我还建议:

  1. 停止使用length ,并使用 numelsize始终如一。 length如果输入是 10x1 或 1x10,则给出相同的答案,并且不适合在连接之前检查数组的维度。
  2. 停止将多个语句放在一行上,尤其是完整的 if声明。如果出现错误,您不知道是哪个语句导致的。
  3. 改进变量命名。当您有名为 A 的变量时,这很难调试,这并不奇怪。 , a , s , ss , as , r , R , rp , p , E , e , ee , idx , ind1 , ind1s , ind1e , ind2s , ind2etmpidx 。读完它让我头疼。

关于c# - 使用 C# 包装器时出现 Matlab 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16196157/

相关文章:

c# - 查找未使用的代码

c# - 运算符==在基础中和在派生中等于

c# - 在 ViewModel 中为命令使用私有(private)集的目的是什么?

variables - 在 MATLAB 中使用 sprintf 显示变量的小数

c# - 使用 OLE-DB 从数据库读取附加文件

c# - 字符串格式化 C# 解码?

C# WCF REST 服务响应 - 删除自动生成的内容

c# - 如何使用 C# 中的内置网页浏览器保存完整的网页

image - Matlab - 缩放图像的颜色条

Matlab:明确指定饼图切片颜色