MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

of memory required to store a structure or cell array depends not only on how much data
it holds, but also on how it is constructed.


For example, take a scalar structure array S1 having fields R, G, and B. Each field of size
100-by-50 requires one array header to describe the overall structure, one header for
each unique field name, and one header per field for the 1-by-1 structure array. This
makes a total of seven array headers for the entire data structure:


S1.R(1:100,1:50)
S1.G(1:100,1:50)
S1.B(1:100,1:50)


On the other hand, take a 100-by-50 structure array S2 in which each element has scalar
fields R, G, and B. In this case, you need one array header to describe the overall
structure, one for each unique field name, and one per field for each of the 5,000
elements of the structure, making a total of 15,004 array headers for the entire data
structure:


S2(1:100,1:50).R
S2(1:100,1:50).G
S2(1:100,1:50).B


Even though S1 and S2 contain the same amount of data, S1 uses significantly less space
in memory. Not only is less memory required, but there is a corresponding speed benefit
to using the S1 format, as well.


See “Cell Arrays” and “Structures” under “Data Structures and Memory” on page 29-16.


Memory Usage Reported By the whos Function


The whos function displays the amount of memory consumed by any variable. For reasons
of simplicity, whos reports only the memory used to store the actual data. It does not
report storage for the array header, for example.


Function Arguments


MATLAB handles arguments passed in function calls in a similar way. When you pass a
variable to a function, you are actually passing a reference to the data that the variable
represents. As long as the input data is not modified by the function being called, the
variable in the calling function and the variable in the called function point to the same
location in memory. If the called function modifies the value of the input data, then
MATLAB makes a copy of the original array in a new location in memory, updates that


How MATLAB Allocates Memory
Free download pdf