Geoscience Reference
In-Depth Information
1 9 3 7
6 6 3 -2
Displaying the elements of A could be problematic for very large arrays such
as digital elevation models consisting of thousands or millions of elements.
To suppress the display of an array or the result of an operation in general,
the line should be ended with a semicolon.
A = [2 4 3 7; 9 3 -1 2; 1 9 3 7; 6 6 3 -2];
h e array A is now stored in the workspace and we can carry out some basic
operations with it, such as computing the sum of elements,
sum(A)
which results in the display
ans =
18 22 8 14
Since we did not specify an output variable, such as A for the array entered
above, MATLAB uses a default variable ans , short for answer or most recent
answer , to store the results of the calculation. In general, we should dei ne
variables since the next computation without a new variable name will
overwrite the contents of ans .
h e above example illustrates an important point about MATLAB: the
sot ware prefers to work with the columns of arrays. h e four results of
sum(A) are obviously the sums of the elements in each of the four columns of
A . To sum all elements of A and store the result in a scalar b , we simply need
to type
Movie
2.1
b = sum(sum(A));
which i rst sums the columns of the array and then the elements of the
resulting vector. We now have two variables, A and b , stored in the workspace.
We can easily check this by typing
whos
which is one the most frequently-used MATLAB commands. h e sot ware
then lists all variables in the workspace, together with information about
their sizes or dimensions, number of bytes, classes and attributes (see Section
2.5 for details about classes and attributes of objects).
Name Size Bytes Class Attributes
A 4x4 128 double
Search WWH ::




Custom Search