Matlab coder Non-constant expression or empty matrix

5 visualizaciones (últimos 30 días)
Mohamed
Mohamed el 4 de Jun. de 2016
Editada: Pasquale el 5 de Nov. de 2024
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 ?

Respuestas (2)

Walter Roberson
Walter Roberson el 5 de Jun. de 2016
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 comentarios
Ayim Manuel De la fuente de Pablo
Ayim Manuel De la fuente de Pablo el 5 de Mzo. de 2018
Hi,
I am facing this same problem, did you get to solve it?
Thanks!
Chris Volpe
Chris Volpe el 28 de Ag. de 2023
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.

Iniciar sesión para comentar.


Pasquale
Pasquale el 5 de Nov. de 2024
Editada: Pasquale el 5 de Nov. de 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.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by