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

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

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

 채택된 답변

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

+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?
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 2019년 10월 20일
편집: Lucademicus 2019년 10월 20일
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!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2017년 4월 20일

댓글:

2019년 10월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by