我制作了 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
, s
和tmp
。这将使您了解为什么它们不能连接 - 我猜测它们具有不同的维度。
我还建议:
- 停止使用
length
,并使用numel
或size
始终如一。length
如果输入是 10x1 或 1x10,则给出相同的答案,并且不适合在连接之前检查数组的维度。 - 停止将多个语句放在一行上,尤其是完整的
if
声明。如果出现错误,您不知道是哪个语句导致的。 - 改进变量命名。当您有名为
A
的变量时,这很难调试,这并不奇怪。 ,a
,s
,ss
,as
,r
,R
,rp
,p
,E
,e
,ee
,idx
,ind1
,ind1s
,ind1e
,ind2s
,ind2e
和tmpidx
。读完它让我头疼。
关于c# - 使用 C# 包装器时出现 Matlab 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16196157/