How can I save the same array with different names (saving RAM memory)?

1 回表示 (過去 30 日間)
Francisco cantos
Francisco cantos 2017 年 4 月 20 日
コメント済み: Lucademicus 2019 年 10 月 21 日
I am working with long arrays that are squeezing my RAM memory. These arrays are stored in structures with long names.
I would like to know if it would be possible to have these arrays saved with shorter names but do not use more RAM memory and still conserve their current name. Like a shortcut. It would be great to work with these arrays with a short name for multiple operations such as mean, plot, etc...
For instance:
Struct1.Struct2.Struct3.Struc4a=rand(1e6,1);
Struct1.Struct2.Struct3.Struc4b=rand(1e6,1);
plot(Struct1.Struct2.Struct3.Struc4a,Struct1.Struct2.Struct3.Struc4b)
Currently I am creating new variables and wasting such a precious memory.
Struct1.Struct2.Struct3.Struc4a=rand(1e6,1);
Struct1.Struct2.Struct3.Struc4b=rand(1e6,1);
Var1=Struct1.Struct2.Struct3.Struc4a;
Var2=Struct1.Struct2.Struct3.Struc4b;
plot(Var1,Var2)
I wonder if it would be possible to save this memory, but still be able to store those values with both variables names. Would it be possible to change some value of the original array and have this change applied automatically in the "shortcut"?
Thanks for your attention. Regards.
  2 件のコメント
Rik
Rik 2017 年 4 月 20 日
You could try an anonymous function, but you might have to redefine that each time you change the original structure.
What is the problem you are trying to solve here? Aesthetics?
Francisco cantos
Francisco cantos 2017 年 4 月 20 日
Yes, it is aesthetics.
I think the code could be more clear and structured if I do not need to mention the whole name of the structure each time I use it.
Anonymous function will not solve it because I will have to call the function with the original name (long names).

サインインしてコメントする。

採用された回答

Jan
Jan 2017 年 4 月 20 日
編集済み: Jan 2017 年 4 月 20 日
Such "shortcuts" are working in Matlab directly:
Struct1.Struct2.Struct3.Struc4a = rand(1e6,1);
Struct1.Struct2.Struct3.Struc4b = rand(1e6,1);
TmpStruct = Struct1.Struct2.Struct3;
Var1 = TmpStruct.Struc4a;
Var2 = TmpStruct.Struc4b;
plot(Var1, Var2)
Neither the creation of TmpStruct nor Var1/2 does duplicate the data. This is called a "shared data copy": While the created variables have an overhead of about 100 bytes, the actual data are shared: Var1 and Struct1.Struct2.Struct3.Struc4a contain pointers, which point to the same section of the RAM.
The data are copied, when they are modified:
Var1(1) = 0;
Then Matlab duplicates the original array and inserts the modification afterwards. This is called "copy on write". But as long as you read the data only, Matlab keeps the shared data.
If you access a nested sub-struct in a loop, such shortcuts can save processing time: addressing TmpStruct.Struc4a is faster (and nicer) than Struct1.Struct2.Struct3.Struc4a.
  7 件のコメント
Bruno Luong
Bruno Luong 2019 年 10 月 20 日
If you can keep the dimensions of your arrays to be consistent, then you could avoid (reduce) the usage of PERMUTE.
It is about design your data structure and know what you would do with it.
I don't know about the documentation, there might be a page somewhere. Personally I use MATLAB for long time and it all comes from the fact that I know how MATLAB organize the data and how the duplication happens and also the fact that MATLAB prefers to work along the first dimension in calculation, and put the data in the form tat you don't need to permute to feed to calculation and prepare for graphic output, etc....
Lucademicus
Lucademicus 2019 年 10 月 21 日
Thanks for your insights Bruno!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by