23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

11 Improving Performance and Memory Usage<br />

As long as the contents of the array are not modified, there is no need to<br />

store two copies of it. If you modify the array, then <strong>MATLAB</strong> does create a<br />

separate array to hold the new values.<br />

If you modify the array shown above by referencing it with variable A<br />

(e.g., A(400,:) = 0), then <strong>MATLAB</strong> creates a copy of the array, modifies it<br />

accordingly, and stores a pointer to the new array in A. Variable B continues<br />

to point to the original array. If you modify the array by referencing it with<br />

variable B (e.g., B(400,:) = 0), the same thing happens except that it is B<br />

that points to the new array.<br />

Array Headers<br />

When you assign an array to a variable, <strong>MATLAB</strong> also stores information<br />

about the array (such as data type and dimensions) in a separate piece of<br />

memory called a header. For most arrays, the memory required to store the<br />

header is insignificant. There is a small advantage though to storing large<br />

data sets in a small number of large arrays as opposed to a large number of<br />

small arrays, as the former configuration requires fewer array headers.<br />

Structure and Cell Arrays. For structures and cell arrays, <strong>MATLAB</strong> creates<br />

a header not only for each array, but also for each field of the structure and for<br />

each cell of a cell array. Because of this, the amount of memory required to<br />

store a structure or cell array depends not only on how much data it holds,<br />

but also how it is constructed.<br />

For example, a scalar structure array S1 having fields R, G, andB, eachfieldof<br />

size 100-by-50, requires one array header to describe the overall structure,<br />

and one header to describe each of the three field arrays, making a total of 4<br />

array headers for the entire data structure:<br />

S1.R(1:100,1:50)<br />

S1.G(1:100,1:50)<br />

S1.B(1:100,1:50)<br />

On the other hand, a 100-by-50 structure array S2 inwhicheachelementhas<br />

scalar fields R, G, andB requires one array header to describe the overall<br />

structure, and one array header per field for each of the 5,000 elements of the<br />

structure, making a total of 15,001 array headers for the entire data structure:<br />

S2(1:100,1:50).R<br />

11-14

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!