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

1 vue (au cours des 30 derniers jours)
Francisco cantos
Francisco cantos le 20 Avr 2017
Commenté : Lucademicus le 21 Oct 2019
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 commentaires
Rik
Rik le 20 Avr 2017
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 le 20 Avr 2017
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).

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 20 Avr 2017
Modifié(e) : Jan le 20 Avr 2017
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 commentaires
Bruno Luong
Bruno Luong le 20 Oct 2019
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 le 21 Oct 2019
Thanks for your insights Bruno!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by