Programming Fundamentals - The MathWorks - #751

/ 840


catalogue search
P. 751
P. 752
P. 753
P. 754
P. 755
P. 756
P. 757
P. 758
P. 759
P. 760
P. 761
P. 762
P. 763
P. 764
P. 765
P. 766
P. 767
P. 768
P. 769
P. 770
P. 771
P. 772
P. 773
P. 774
P. 775
P. 776
P. 777
P. 778
P. 779
P. 780
P. 781
P. 782
P. 783
P. 784
P. 785
P. 786
P. 787
P. 788
P. 789
P. 790
P. 791
P. 792
P. 793
P. 794
P. 795
P. 796
P. 797
P. 798
P. 799
P. 800


See other catalogues for The MathWorks

Text version of the page
Strategies for Efficient Use of Memory
Because simple numeric arrays (comprising one mxArray)havethe least overhead, you should use them wherever possible. When data is too complex to store in a simple array (or matrix), you can use other data structures.
Cell arrays are comprised of separate mxArrays for each element. As a result, cell arrays with many small elements have a large overhead.
Structures require a similar amount of overhead per field (see the documentation on "Array Headers" on page 11-4 above). Structures with many fields and small contents have a large overhead and should be avoided. A large array of structures with numeric scalar fields requires much more memory than a structure with fields containing large numeric arrays.
Also note that while MATLAB stores numeric arrays in contiguous memory, this is not the case for structures and cell arrays.
Import Data to the Appropriate MATLAB® Class
When reading data from a binary file with fread, it is a common error to specify only the class of the data in the file, and not the class of the data MATLAB uses once it is in the workspace. As a result, the default double is used even if you are reading only 8-bit values. For example,
fid = fopen('large_file_of_uint8s.bin', 'r');
a = fread(fid, 1e3, 'uint8'); % Requires 8k
whos a
Name Size Bytes Class Attributes
a 1000x1 8000 double
a = fread(fid, 1e3, 'uint8=>uint8'); % Requires 1k
whos a
Name Size Bytes Class Attributes
a 1000x1 1000 uint8
Make Arrays Sparse When Possible
If your data contains many zeros, consider using sparse arrays, which store only nonzero elements. The example below compares the space required for storageofanarray ofmainlyzeros:
11-17

pageCatalog pdf di En 2012-06-22-01