Assigning values to the array of Enumeration Class seems to be very slow
14 ビュー (過去 30 日間)
古いコメントを表示
Hi, I met this problem when parsing a gps log.
I made an Enumeration Class GpsState as follows.
classdef GpsState
enumeration
Lost
Single
RTD
RTK_Float
RTK_Fix
end
end
Then I created an array of GpsState to record all the states of the log. The process was painfully slow.
Finally, I found out what the problem was caused by the assign of Enumeration Class array.
I simplified the code as follows, the process takes about 4.6s.
tic
test_num = 1e5;
gps_state = repmat(GpsState.Lost,test_num,1);
for ii = 1:test_num
gps_state(ii) = GpsState.RTK_Fix;
end
toc
Is there any problem with my usage mode of Enumeration Class?
Thanks for your reading.
0 件のコメント
回答 (1 件)
Steven Lord
2020 年 1 月 20 日
Why are you initializing all the elements in gps_state to GpsState.Lost in one call them immediately overwriting them with GpsState.RTK_Fix?
If you oversimplified your problem, and you wanted say) to only overwrite certain elements of gps_state, there's no need for a for loop here. Just overwrite them in one fell swoop, something like this untested code.
test_num = 1e5;
gps_state = repmat(GpsState.Lost, test_num, 1);
OneThousandRandomPoints = randperm(test_num, 1000);
gps_state(OneThousandRandomPoints) = GpsState.RTK_Fix; % scalar expansion
参考
カテゴリ
Help Center および File Exchange で Enumerations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!