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

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 comentarios

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?
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).

Iniciar sesión para comentar.

 Respuesta aceptada

Jan
Jan el 20 de Abr. de 2017
Editada: Jan el 20 de Abr. de 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 comentarios

+1 nice explanation.
Thank you for answering!
Does this also apply when reshaping a matrix?
Say I have a large matrix A and I reshape it:
B = reshape(permute(A,[1,3,2]),[],size(A,2));
Is this, in terms of speed and memory consumption, good practice?
Or could I better save the result in A?
Bruno Luong
Bruno Luong el 20 de Oct. de 2019
Editada: Bruno Luong el 20 de Oct. de 2019
Definitively, sharing applies when using RESHAPE as well. This script shows A, and b share the same data (Pointer address 2bc9eac0 identical). That's why RESHAPEis recommended to be used without reservation.
> clear
>> A=rand(2,3);
>> b=reshape(A,[6 1]);
>> format debug
>> A
A =
Structure address = e4bf0f00
m = 2
n = 3
pr = 2bc9eac0
0.6787 0.7431 0.6555
0.7577 0.3922 0.1712
>> b
b =
Structure address = e4d7cb10
m = 6
n = 1
pr = 2bc9eac0
0.6787
0.7577
0.7431
0.3922
0.6555
0.1712
However PERMUTE can't share, since data are reordered.
Lucademicus
Lucademicus el 20 de Oct. de 2019
Editada: Lucademicus el 20 de Oct. de 2019
So since I'm using both reshape and permute in my example, what do you recommend?
Also, regarding your last statement, where can I learn such things from the documentation?
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....
Thanks for your insights Bruno!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 20 de Abr. de 2017

Comentada:

el 21 de Oct. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by