Optimizing matlab code by removing nested for loop? -


this code given, takes long run. how make faster removing nested loop?

    igroup = 1:length(groupindices)             curgroupindex = groupindices(igroup);             curchanindices = chanindices{igroup};             curchannames   = channames{igroup};              grouppropstruct = propstostruct(propnames{curgroupindex},propvalues{curgroupindex},replace_str,prepend_str,always_prepend);             groupstruct = struct('name',groupnames(igroup),'props',grouppropstruct);             ichan = 1:length(curchanindices)                 curchanindex = curchanindices(ichan);                 chanpropstruct = propstostruct(propnames{curchanindex},propvalues{curchanindex},replace_str,prepend_str,always_prepend);                 chanstruct =  struct('name',curchannames{ichan},'props',chanpropstruct,...                     'data',[]);                 chanstruct.data = data{curchanindex};                 groupstruct.(tdms_genvarname2(chanstruct.name,...                     replace_str,prepend_str,always_prepend)) = chanstruct;             end             output.(tdms_genvarname2(groupstruct.name,...                 replace_str,prepend_str,always_prepend)) = groupstruct;         end 

as 1 said, it's hard without knowing, code actual doing. maybe give toy-example regarding code? help.

anyway, here 4 major points consider, when writing matlab for-loops:

1: of course, use build-in matlab functionality instead of for-loops. written in c/fortran , faster regarding simd, multythreading, etc.

2: for-loops consecutiv , sliceable. consider use parfor loops, use multi-processor functionality on loop.

3: for-loop capsuled in matlab-function? if not, it!, yit-compiler matlab can compile loop byte-code faster!

4: if familiar c++, write mex-function. here can use full potential of machine.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -