Specify output data type of "find" function
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
dpb
2022-10-22
" 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 个评论
dpb
2022-10-23
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.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!