MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

11 Improving Performance and Memory Usage As long as the contents of the array are not modified, there is no need to store two copies of it. If you modify the array, then MATLAB does create a separate array to hold the new values. If you modify the array shown above by referencing it with variable A (e.g., A(400,:) = 0), then MATLAB creates a copy of the array, modifies it accordingly, and stores a pointer to the new array in A. Variable B continues to point to the original array. If you modify the array by referencing it with variable B (e.g., B(400,:) = 0), the same thing happens except that it is B that points to the new array. Array Headers When you assign an array to a variable, MATLAB also stores information about the array (such as data type and dimensions) in a separate piece of memory called a header. For most arrays, the memory required to store the header is insignificant. There is a small advantage though to storing large data sets in a small number of large arrays as opposed to a large number of small arrays, as the former configuration requires fewer array headers. Structure and Cell Arrays. For structures and cell arrays, MATLAB creates a header not only for each array, but also for each field of the structure and for each cell of a cell array. Because of this, the amount of memory required to store a structure or cell array depends not only on how much data it holds, but also how it is constructed. For example, a scalar structure array S1 having fields R, G, andB, eachfieldof size 100-by-50, requires one array header to describe the overall structure, and one header to describe each of the three field arrays, making a total of 4 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, a 100-by-50 structure array S2 inwhicheachelementhas scalar fields R, G, andB requires one array header to describe the overall structure, and one array header per field for each of the 5,000 elements of the structure, making a total of 15,001 array headers for the entire data structure: S2(1:100,1:50).R 11-14

Using Memory Efficiently S2(1:100,1:50).G S2(1:100,1:50).B Thus, 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. 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 variable itself or the array header. 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 pointer 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 copy with the modified value, and points the input variable in the called function to this new array. In the example below, function myfun modifies the value of the array passed into it. MATLAB makes a copy in memory of the array pointed to by A, sets variable X as a pointer to this new array, and then sets one row of X to zero. The array referenced by A remains unchanged: A = magic(500); myfun(A); function myfun(X) X(400,:) = 0; If the calling function needs the modified value of the array it passed to myfun, you will need to return the updated array as an output of the called function, as shown here for variable A: A = magic(500); A = myfun(A); 11-15

Using Memory Efficiently<br />

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

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

Thus, even though S1 and S2 contain the same amount of data, S1 uses<br />

significantly less space in memory. Not only is less memory required, but<br />

there is a corresponding speed benefit to using the S1 format as well.<br />

Memory Usage Reported By the whos Function. The whos function<br />

displays the amount of memory consumed by any variable. For reasons of<br />

simplicity, whos reports only the memory used to store the actual data. It does<br />

not report storage for the variable itself or the array header.<br />

Function Arguments<br />

<strong>MATLAB</strong> handles arguments passed in function calls in a similar way. When<br />

you pass a variable to a function, you are actually passing a pointer to the<br />

data that the variable represents. As long as the input data is not modified<br />

by the function being called, the variable in the calling function and the<br />

variable in the called function point to the same location in memory. If the<br />

called function modifies the value of the input data, then <strong>MATLAB</strong> makes<br />

a copy of the original array in a new location in memory, updates that copy<br />

with the modified value, and points the input variable in the called function<br />

to this new array.<br />

In the example below, function myfun modifies the value of the array passed<br />

into it. <strong>MATLAB</strong> makes a copy in memory of the array pointed to by A, sets<br />

variable X as a pointer to this new array, and then sets one row of X to zero.<br />

The array referenced by A remains unchanged:<br />

A = magic(500);<br />

myfun(A);<br />

function myfun(X)<br />

X(400,:) = 0;<br />

If the calling function needs the modified value of the array it passed to<br />

myfun, you will need to return the updated array as an output of the called<br />

function, as shown here for variable A:<br />

A = magic(500);<br />

A = myfun(A);<br />

11-15

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

Saved successfully!

Ooh no, something went wrong!