MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
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
reference 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 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);
sprintf('The new value of A is %d', A)

function Y = myfun(X)
X(400,:) = 0;
Y = X;

Data Structures and Memory


Memory requirements differ for the various types of MATLAB data structures. You might
be able to reduce the amount of memory used for these structures by considering how
MATLAB stores them.

Numeric Arrays

MATLAB requires 1, 2, 4, or 8 bytes to store 8-bit, 16-bit, 32-bit, and 64-bit signed and
unsigned integers, respectively. For floating-point numbers, MATLAB uses 4 or 8 bytes for
single and double types. To conserve memory when working with numeric arrays,
MathWorks recommends that you use the smallest integer or floating-point type that
contains your data without overflowing. For more information, see “Numeric Types”.

29 Memory Usage

Free download pdf