Matlab coder Non-constant expression or empty matrix
5 次查看(过去 30 天)
显示 更早的评论
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 ?
0 个评论
回答(2 个)
Walter Roberson
2016-6-5
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 个评论
Ayim Manuel De la fuente de Pablo
2018-3-5
Hi,
I am facing this same problem, did you get to solve it?
Thanks!
Chris Volpe
2023-8-28
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.
Pasquale
2024-11-5
编辑:Pasquale
2024-11-5
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!