How to use memoization in MatLab?
12 ビュー (過去 30 日間)
古いコメントを表示
if I have some function substantially more complicated than
function out = y(x)
out = 2*x;
end
but let's just go with this one... is there a way to save the value y(1) for example, so that if I call y(5) and then y(1) again, it's not going to CALCULATE y(1) but pull it from memory? Maybe have a function create a separate variable "y1" and store it? The problem is that I would like to not have to create empty variable BEFORE running the function (because i may not know what names they should have), i want it to create them by itself. Is there a way to do it? I guess one way would be to maybe create an empty array "y" and then store each value in its respective cell? So solution to y(1) would go to cell #1 in the array, and so on... This doesn't feel right tho. And I still don't know how that would prevent the function from doing the same calculation over and over again rather than just doing it once.
Any hints would be appreciated :]
0 件のコメント
回答 (2 件)
Prabhakar
2018 年 7 月 24 日
Memoize was added to MATLAB in R2017a.
The documentation is a good resource to begin with, or if you prefer here is a blog which walks you through it.
1 件のコメント
Walter Roberson
2018 年 7 月 24 日
For earlier versions
See also https://www.mathworks.com/matlabcentral/answers/345464-recurrence-relation-for-any-given-n#comment_467043 in which I present the memoized version of a recursive relationship, and code that emulates memoize() for use with earlier versions.
参考
カテゴリ
Help Center および File Exchange で Performance and Memory についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!