Codegen and the "find" Function

8 visualizzazioni (ultimi 30 giorni)
Greg
Greg il 6 Set 2012
I'm writing a piece of Matlab code that will be used in a Simulink "Matlab Function" block, meaning that it will get run through the Matlab Coder.
When I use the "find" function, even when I know exactly the size that it's output is going to be, I can't store the answer in a fixed-size piece of memory because the Coder doesn't ever know what the output size is going to be.
Assume in the following code that we've already guaranteed that ind_vector is monotonically-increasing:
if query(i) < ind_vector(1)
locations(i) = 0;
else
location(i) = find(query(i)>=ind_vector,1,'last');
end
Because I've guaranteed that ind_vector is monotonically-increasing, I've checked for the condition that the query is lower than the whole vector (which is the only case that would make "find" return empty), and I've only asked find for a single output, I know that it will return a 1x1. The Coder does not know this, however, and considers the output a ?x1 and thus won't let me store it in a 1x1 slot.
Is there any way to prommise the Coder that this output is a specific size? I tried using assert, but it didn't work.
Thanks

Risposta accettata

Mike Hosea
Mike Hosea il 6 Set 2012
Try
tmp = find(etc.);
location(i) = tmp(1);
However, FIND is pretty heavyweight for this. I prefer to write little helper functions to perform tasks like this because I can avoid creating a Boolean temporary array and because it avoids the variable sizing issue (since I can return 0 to mean "not found").
  2 Commenti
Greg
Greg il 6 Set 2012
Thanks, that works! Actually, I feel a little silly now because I've used that trick in the past to make other things work; had just forgotten about it.
I use FIND here because, in the non-codegen case, it's noticeably faster than the helper function approach that I take in some cases where FIND doesn't work for me. I would guess that speed advantage goes away when the code is compiled, though.
Mike Hosea
Mike Hosea il 6 Set 2012
Modificato: Mike Hosea il 6 Set 2012
Yes. The speed situation is probably inverted in this case. The best practices when coding for MATLAB Coder are not the same as the best practices coding for MATLAB. Someday they may be the same, but not today. Another good example of this is related--logical indexing. Logical indexing works in MATLAB Coder, but it does not generate the kind of C code you would have written by hand to accomplish the same task. Rather it expresses in C the literal meaning of the statements in the MATLAB code. If the difference is big enough to warrant it in a bottleneck section of code, you can choose a different method for MATLAB versus MATLAB Coder based on isempty(coder.target).

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su MATLAB Coder in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by