2

I've made a dll version of a clustering algorithm implemented in matlab

Also, I've downloaded an working sample of the input data (I'm using the Toy Problem data) and I'm reading it and also converting it to a Matlab known data type.

However, when running the algorithm I get the following error:

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

Here is my code: (sorry?)

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();
    }

Thanks.

3
  • Have you tested it in purely MATLAB? Sounds like it's expecting a input of a different shape than what you're giving it (e.g. column vs. row vector). Commented Apr 24, 2013 at 15:41
  • Yes, I did. Actually this samples are for online demos that use this algorithm... Commented Apr 24, 2013 at 15:51
  • Actually, I've tried with more than one example and I get the same error. Commented Apr 24, 2013 at 15:57

2 Answers 2

2

From a quick look at line 178 of your code, it looks like you're concatenating an Nx2 array with p, and then concatenating that with s, where sometimes N is defined as length(p), sometimes as size(s,1) and sometimes as something called tmp.

I'm not going to debug this, but I would suggest that to do so you should modify your code so that before line 178 it displays, or somehow outputs, the values of N, p, s and tmp. This will give you an idea of why they can't be concatenated - I'm guessing that they have different dimensions.

I'd also suggest:

  1. Stop using length, and use either numel or size consistently. length gives you the same answer if its input is 10x1 or 1x10, and is not the right thing to use to check the dimensions of an array before concatenating.
  2. Stop putting multiple statements on a single line, especially complete if statements. If there's an error, you don't know which statement caused it.
  3. Improve your variable naming. It's not surprising this is tough to debug when you have variables called A, a, s, ss, as, r, R, rp, p, E, e, ee, idx, ind1, ind1s, ind1e, ind2s, ind2e and tmpidx. It makes my head hurt to read through it.
Sign up to request clarification or add additional context in comments.

2 Comments

How I understand your comments. However note that this code is not mine and i've tried it (and working) with the algorithm inner samples. However, only when I try to run it in C# (after I've generated the dll with Matlab Builder NE) I get this error...
So if it's the exact same code, just compiled, then C# must be giving it different inputs. Perhaps it's passing matlabPreferencesArray as an Nx1 rather than 1xN, or maybe matlabSimilaritiesArray is a vector of length (NxN) instead of an NxN array. Like I suggested, modify the code so that it displays its state just before the erroring line 178 - then you'll be able to tell why MATLAB cannot do the concatenation.
0

The problem was with the argument P must not be an array (as is on the website input) but instead, a scalar.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.