Specify output data type of "find" function
3 views (last 30 days)
Show older comments
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'))
class(find(single(a)>.5,1,'first'))
class(find(single(a>.5),1,'first'))
0 Comments
Accepted Answer
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
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.
More Answers (0)
See Also
Categories
Find more on Simulink Coder in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!