MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
To predict the size of an entire cell array, multiply the number you have derived for the
header by the total number of cells in the array, and then add to that the number of bytes
required for the data you intend to store in the array:

(header_size x number_of_cells) + data

So a 10-by-20 cell array that contains 400 bytes of data would require 22,800 bytes of
memory on a 64-bit system:

(112 x 200) + 400 = 22800

NoteWhile numeric arrays must be stored in contiguous memory, structures and cell
arrays do not.

Example 1 – Memory Allocation for a Cell Array

The following 4-by-1 cell array records the brand name, screen size, price, and on-sale
status for three laptop computers:

Laptops = {['SuperrrFast 89X', 'ReliablePlus G5', ...
'UCanA4dIt 140L6']; ...
[single(17), single(15.4), single(14.1)]; ...
[2499.99, 1199.99, 499.99]; ...
[true, true, false]};

On a 32-bit system, the cell array header alone requires 60 bytes per cell:

4 cells * 60 bytes per cell = 240 bytes for the cell array

Calculate the memory required to contain the data in each of the four cells:

45 characters * 2 bytes per char = 90 bytes
3 doubles * 8 bytes per double = 24 bytes
3 singles * 4 bytes per single = 12 bytes
3 logicals * 1 byte per logical = 3 bytes

90 + 24 + 12 + 3 = 129 bytes for the data

Add the two, and then compare your result with the size returned by MATLAB:

240 + 129 = 369 bytes total

whos Laptops

29 Memory Usage

Free download pdf