Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7524759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:08:59+00:00 2026-05-30T03:08:59+00:00

I’m writing a simulation in Matlab. I will eventually run this simulation hundreds of

  • 0

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 these cycles, I calculate a very complex function, which takes ~0.5 sec to finish.
The function input is a long bit array (>1000 bits) – which is an array of 0 and 1.
I hold the bit arrays in a matrix of 0 and 1, and for each one of them I only run the function once – as I save the result in a different array (res) and check if the bit array is in the matrix before running the functions:

for i=1:1000000000
    %pick a bit array somehow
    [~,indx] = ismember(bit_array,bit_matrix,'rows');
    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

I have two quesitons, really:

  1. Is there a more efficient way to find the index of a row in a matrix then ‘ismember’?

  2. Since I run the simulation many times, and there is a big overlap in the bit-arrays I’m getting, I want to cache the matrix between runs so that I don’t recalculate the function over the same bit-arrays over and over again. How do I do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:09:00+00:00Added an answer on May 30, 2026 at 3:09 am

    The answer to both questions is to use a map. There are a few steps to do this.

    1. First you will need a function to turn your bit_array into either a number or a string. For example, turn [0 1 1 0 1 0] into '011010'. (Matlab only supports scalar or string keys, which is why this step is required.)

    2. Defined a map object

      cachedRunMap = containers.Map;  %See edit below for more on this
      
    3. To check if a particular case has been run, use iskey.

      cachedRunMap.isKey('011010');
      
    4. To add the results of a run use the appending syntax

      cachedRunMap('011010') = [0 1 1 0 1];  %Or whatever your result is.  
      
    5. To retrieve cached results, use the getting syntax

      tmpResult = cachedRunMap.values({'011010'});
      

    This should efficiently store and retrieve values until you run out of system memory.


    Putting this together, now your code would look like this:

    %Hacky magic function to convert an array into a string of '0' and '1'
    strFromBits = @(x) char((x(:)'~=0)+48); %'
    
    %Initialize the map
    cachedRunMap = containers.Map;
    
    %Loop, computing and storing results as needed
    for i=1:1000000000
        %pick a bit array somehow
        strKey = strFromBits(bit_array);
        if cachedRunMap.isKey(strKey)
            result = cachedRunMap(strKey);
        else
            result = complex_function(bit_array);
            cachedRunMap(strKey) = reult;
        end
        %do something with result
    end
    

    If you want a key which is not a string, that needs to be declared at step 2. Some examples are:

    cachedRunMap = containers.Map('KeyType', 'char', 'ValueType', 'any');
    cachedRunMap = containers.Map('KeyType', 'double', 'ValueType', 'any');
    cachedRunMap = containers.Map('KeyType', 'uint64', 'ValueType', 'any');
    cachedRunMap = containers.Map('KeyType', 'uint64', 'ValueType', 'double');
    

    Setting a KeyType of 'char' sets the map to use strings as keys. All other types must be scalars.


    Regarding issues as you scale this up (per your recent comments)

    • Saving data between sessions: There should be no issues saving this map to a *.mat file, up to the limits of your systems memory

    • Purging old data: I am not aware of a straightforward way to add LRU features to this map. If you can find a Java implementation you can use it within Matlab pretty easily. Otherwise it would take some thought to determine the most efficient method of keeping track of the last time a key was used.

    • Sharing data between concurrent sessions: As you indicated, this probably requires a database to perform efficiently. The DB table would be two columns (3 if you want to implement LRU features), the key, value, (and last used time if desired). If your “result” is not a type which easily fits into SQL (e.g. a non-uniform size array, or complex structure) then you will need to put additional thought into how to store it. You will also need a method to access the database (e.g. the database toolbox, or various tools on the Mathworks file exchange). Finally you will need to actually setup a database on a server (e.g. MySql if you are cheap, like me, or whatever you have the most experience with, or can find the most help with.) This is not actually that hard, but it takes a bit of time and effort the first time through.

      Another approach to consider (much less efficient, but not requiring a database) would be to break up the data store into a large (e.g. 1000’s or millions) number of maps. Save each into a separate *.mat file, with a filename based on the keys contained in that map (e.g. the first N characters of your string key), and then load/save these files between sessions as needed. This will be pretty slow … depending on your usage it may be faster to recalculate from the source function each time … but it’s the best way I can think of without setting up the DB (clearly a better answer).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I am writing an app with both english and french support. The app requests

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.