Is there an efficient way to calculate hash for objects

Hey,
I have objects ("Sequence") on which I need to do some heavy calculations, the result is another object of the same kind (a different, fixed, "Sequence").
These calculations are often repeated, so I want to save the results in a Map, such that they will only be done once (for each Sequence).
The problem: The keys for the map have to be numbers or strings...
Is there any Efficient way to calculate some sort of unique Hash for an object?
I've seen Datahash, but it seems to be for structs...
If it matters, "Sequence" contains only a single property: a cell array of "Pulse" (another object). "Pulse" has several primitive properties and a struct.
Small clarification, I need the hash to be the same for different handles if internal structs are the same (same as isequal, and not as ==).
For example:
a = Sequence() % Default constructor
b = Sequence() % Default constructor
Hash(a) == Hash(b) % Should be true
Of course, I can calculate some sort of hash myself, but I wonder if there is some other way which might be more efficient...

 Risposta accettata

Have a look at the memorized function: https://www.mathworks.com/help/matlab/ref/memoizedfunction.html. This might be helpful. The submission by Jan, you linked in your question, will also work for objects, not just structs. You may also want to have a look at jsonencode(): https://www.mathworks.com/help/matlab/ref/jsonencode.html

7 Commenti

The memorized function looks promising! It says it works when the input is the same, I wonder if it means == or isequal (which will make all the difference for me). I will test it tomorrow! I'll also try the datahash function, and create a simple hash/id myself, and see which is faster.
Thanks
From the documentation, it appears that memorize() will only work for numeric inputs; however, I haven't tried it. Maybe you can try a few cases and post the result here. It will be good for future reference.
So none of you suggested methods worked off the bat :|
memorize() only works for numeric inputs (I didn't understand it from the docs), and since the function only accepts the object, it looks at the handle and always returns the same thing :(
Datahash doesn't work for objects, it asks you to manually implement a converation from object to hash. Not that useful! However, hidden inside the documention was an undocumented matlab function which can actually work!
It is getByteStreamFromArray. You can read more about it over here:
This actually solves my problem completely...
I modified DataHash to make it use getByteStreamFromArray (It was simply commenting two lines and uncommenting two other). I then used it on my object to create a key, which I use for the map.
My run time was improved drastically!
It is actually easier and faster to do char(getByteStreamFromArray(obj)) and use that as the key. But the resulting string is ~2000 characters long, and I decided it's not worth the memory...
Ah!! The undocumented MATLAB. Glad that you found a solution. If you already have the bitstream then you may avoid Datahash. You can directly use md5 hashing function provided by MATLAB: https://www.mathworks.com/help/releases/R2020a/rptgen/ug/mlreportgen.utils.hash.html, but I guess that requires a separate toolbox. You can select whatever option is more convenient.
Nice!
Unfortunately, I'm currently using R2017b, and this function was only introduced in 2018a...
So I'll stick with DataHash for now.
Folks: please note the difference between the words "memorized" and "memoized" (no "r"). The name of the function is "MemoizedFunction".
Coming back to a question that was answered 4 years ago to comment about a typo in a function that wasn't used for the solution. So helpful, thanks.

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by