Search code examples
matlabcompilationreshapematlab-coder

Reshape function output size is unknown


I am using Matlab coder to compile some .m files into C static library. In the function below, I am getting the following errors:

 function net = mlpunpak(net, w)
    nin = net.nin;
    nhidden = net.nhidden;
    nout = net.nout;

    mark1 = nin*nhidden;
    net.w1 = reshape(w(1:mark1), nin, nhidden);          % Error1 ***
    mark2 = mark1 + nhidden;
    net.b1 = reshape(w(mark1 + 1: mark2), 1, nhidden);   % Error2 ***
    mark3 = mark2 + nhidden*nout;
    net.w2 = reshape(w(mark2 + 1: mark3), nhidden, nout);% Error3 ***
    mark4 = mark3 + nout;
    net.b2 = reshape(w(mark3 + 1: mark4), 1, nout);      % Error4 ***

Error1: Dimension 1 is fixed on the left-hand side but varies on the right ([10 x 8] ~= [:? x :?]). Error2: Dimension 1 is fixed on the left-hand side but varies on the right ([8 x 1] ~= [:? x :?]). Error3: Dimension 1 is fixed on the left-hand side but varies on the right ([8 x 1] ~= [:? x :?]). Error4: Dimension 2 is fixed on the left-hand side but varies on the right ([1 x 1] ~= [1 x :?]).

The value of the variables are nin=10, nhidden=8, nout=1 and this function overwrites the fileds of the net. Any help is highly appreciated.


Solution

  • I think you gave the fields w1, b1, w2, b2 the fixed dimensions somewhere. In this case, you are using a variable-size array as input of reshape, that causes the problem. Have a look at this.

    UPDATE: ok, i think i solved the errors. In the Overview tab of Matlab coder, i tried to define the fields as matrix of double with unbounded dimensions. And whoops, Code generation successful: View report :-)

    Btw, at the Error 2, i think it's your fault, since the output of reshape here should be 1x8, you have to check it yourself regarding to your algorithm.

    enter image description here