Caching Matlab function results to file

前端 未结 3 624
旧巷少年郎
旧巷少年郎 2021-01-20 15:38

I\'m writing a simulation in Matlab. I will eventually run this simulation hundreds of times. In each simulation run, there are millions of simulation cycles. In each of the

3条回答
  •  滥情空心
    2021-01-20 16:09

    I think that you should use containers.Map() for the purpose of speedup.

    The general idea is to hold a map that contains all hash values. If your bit arrays have uniform distribution under the hash function, most of the time you won't need the call to ismember.

    Since the key type cannot be an array in Matlab, you can calculate some hash function on your array of bits.

    For example:

     function s = GetHash(bitArray)
          s = mod( sum(bitArray), intmax('uint32'));          
     end
    

    This is a lousy hash function, but enough to understand the principle. Then the code would look like:

    map = containers.Map('KeyType','uint32','ValueType','any');
    for i=1:1000000000
        %pick a bit array somehow
        s = GetHash(bit_array);   
        if isKey  %Do the slow check.
            [~,indx] = ismember(bit_array,bit_matrix,'rows');
        else
           map(s) = 1;
           continue;
        end
        if indx == 0
            indx = length(results) + 1;
            bit_matrix(indx,:) = bit_array;
            res(indx) = complex_function(bit_array);
        end
        result = res(indx)
        %do something with result
    end
    

提交回复
热议问题