Is there an efficient way to calculate hash for objects

조회 수: 26 (최근 30일)
Yoav Romach
Yoav Romach 2020년 4월 27일
댓글: Yoav Romach 2024년 6월 21일
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...

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 4월 27일
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
Alan Frankel
Alan Frankel 2024년 6월 20일
Folks: please note the difference between the words "memorized" and "memoized" (no "r"). The name of the function is "MemoizedFunction".
Yoav Romach
Yoav Romach 2024년 6월 21일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by