Specify output data type of "find" function

2 views (last 30 days)
I'm working in simulink and I am running some heavy image processing on a lightweight 32 bit drone. Is there a way to specify the output data type of find? I can convert the row/col indecies to singles after, but it seems like a waste to not set that beforehand. Changing the input matrix type seems to have no effect. Is there something akin to the "like" functionality when using "zeros" or "ones"?
a = rand(5);
class(find(a>.5,1,'first'))
ans = 'double'
class(find(single(a)>.5,1,'first'))
ans = 'double'
class(find(single(a>.5),1,'first'))
ans = 'double'

Accepted Answer

dpb
dpb on 22 Oct 2022
" Is there something akin to the "like" functionality [in find]...?"
No.
Everything numeric including indices in MATLAB is a double unless cast to something else...since it will come back as double to begin with and we're only talking a single value , I'd suspect that doing the cast will be more expensive than the cost of the memory. Even if you were to do something like
ix=zero(1,'single'); % preallocate for the index to come later
ix=find(a>0.5,1); % try to assign a double into a single
MATLAB will promote ix to double. Fortran would do t'other way 'round, but MATLAB isn't Fortran.
As you note, the only way if it is imperative to return a single in the output variable is the explicit cast...
ix=single(find(a>0.5,1));
and then it might be better to use uint32 than floating point if the hardware is really limited.
Are we actually running MATLAB here or generating code to run on another processor?
  2 Comments
Samuel Kemp
Samuel Kemp on 22 Oct 2022
"I'd suspect that doing the cast will be more expensive..."
For curiosity's sake, is there a way to know how much the cast costs? When I cast these values to singles, it is only so that the subsequent operations are performed on singles (to avoid promotions).
And this is generating code to run on another processor. I'm doing it for the Mathworks Minidrone competition, so I'm not 100% sure how the model works, but I know everything needs to be C/C++ code generation compatible.
dpb
dpb on 23 Oct 2022
I've no klew about what the mindrone is, no, but it would depend on what its processor has for an instruction set -- if it doesn't have floating point instruction set, then singles aren't all that efficient, either.

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!