Matlab coder Non-constant expression or empty matrix

Hello, I am trying to make a function made by Matlab transformed into MEX file to enhance execution speed of the function but the coder show the error:
Non-constant expression or empty matrix. This expression must be constant because its value determines the size or class of some expression. The part of the code that causes the problem is
indx=mat==max(mat);
nominees={acceptedB{indx,:}};
I also tried not to use logical indexing using
indx=find(mat==max(mat));
but the same error appeared. Any idea how to solve this problem without affecting execution speed of the function ?

Answers (2)

First assign to the variable an array that is of the right class and is the maximum size that could possibly be encountered. For example,
indx = zeros(numel(mat),1);
You need to use numel of mat because of the possibility that all of the elements in mat are the same value so your find() might return every index.
Then you can do
indx = find(mat(:) == max(mat(:)));
The result might well be shorter than the size you originally allocated, and that is okay: you can reassign a matrix as shorter but not as longer.
However, I am concerned because it looks to me as if
nominees={acceptedB{indx,:}}
is indefinite size. Would
nominees = acceptedB(index,:);
be acceptable instead?

7 Comments

Thanks for the reply, unfortunately that didn't work and it give the same error and highlights the indx in
nominees={acceptedB{indx,:}};
even when the size of indx is defined.
also I can't use smooth parentheses () with cell arrays because it is not supported in code generation.
nrow = length(indx);
ncol = size(acceptedB,2);
nominees = cell(nrow, ncol);
for R = 1 : nrow
fromrow = indx(R);
for C = 1 : ncol
nominees{R, C} = acceptedB{fromrow, C};
end
end
Thanks for the reply.
Code generation is not supported for variable-size cell arrays constructed via the cell function.
also 2 for loops is not what i am looking for I want to make a vectorized solution as it is crucial to make this function faster and that's why I am making it MEX in the first place.
I doubt that you are going to be able to do it by way of MATLAB Coder; you are going to have to hand-write the C. MATLAB Coder is aimed at stand-alone code that does not have the MATLAB infrastructure, whereas MEX files have the full power of MATLAB to draw on (by calling back into MATLAB if necessary.)
My above code was not correct, by the way: it should have been
nrow = length(indx);
ncol = size(acceptedB,2);
nominees = cell(1, nrow*ncol);
for R = 1 : nrow
fromrow = indx(R);
for C = 1 : ncol
nominees{1, R + (C-1)*nrow} = acceptedB{fromrow, C};
end
end
You could push this further using a single loop that went from 1 to nrow * ncol:
for K = 1 : nrow * ncol
nominees{1, K} = acceptedB{ indx(mod(K-1,nrow)+1), floor((K-1)/nrow)+1 };
end
I would tend to doubt that you would be permitted to vectorize this, as the vectorized version would require either using a left hand side of
[nominess{1,1:end}]
or else using () for the cell array and you say that using () for the cell array is not permitted. If using () for cell array items was permitted then you could vectorize, I think.
Thanks again for your try but it looks like the Matlab coder does not allow non constant array sizes because I got the same error trying your 2nd code. There is another problem that the nominees cell array should be nx2 not a single row vector but I can try to reshape it if it actually compiles in the first place
Hi,
I am facing this same problem, did you get to solve it?
Thanks!
I am having the same error, but in the context of referencing a struct field whose name is contained in a variable, i.e. an experssion of the form "foo.(bar)". However, for the present context, the proposed solution seems to suggest using non-curly braces to index into a cell array, which Coder considers gauche.

Sign in to comment.

Pasquale
Pasquale on 5 Nov 2024
Edited: Pasquale on 5 Nov 2024
In my case I had the same error using a Matlab Function. The code inside preallocated the output matrix to force the right sw type (single) and size (pred_hor x ny), with pred_hor and ny being workspace parameters:
Ref = single(zeros(pred_hor,ny));
I got that error on the above line, and I solved it by going to data editor ("Edit Data") for the matlab function, and specifying that the two parameters are not tunable (I had to deselect the corresponding chck box). This allowed the parser to infer the right dimension and run correctly.

Products

Asked:

on 4 Jun 2016

Edited:

on 5 Nov 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!