Geoscience Reference
In-Depth Information
B =
8 2 7
9 10 0
1 10 8
9 5 9
6 8 7
1 1 8
3 4 7
5 9 4
10 8 7
10 10 2
h is topic tries to make all of the recipes independent of the actual
dimensions of the data. h is is achieved by the consistent use of
size
and
length
to determine the size of the data instead of using i xed numbers such
as the
30
and
3
in the above example (Section 2.4).
rng(0)
A = rand(10,3)
for i = 1 : size(A,1)
for j = 1 : size(A,2)
B(i,j) = round(10 * A(i,j));
end
end
B
When working with larger data sets with many variables one might
occasionally wish to automate array manipulations such as those described
in Section 2.4. Let us assume, for example, that we want to replace all
NaN
s
in all variables in the memory with
-999
. We i rst create a collection of four
variables, each of which contains a single
NaN
.
clear
rng(0)
A = rand(3,3); A(2,1) = NaN
BC = rand(2,4); BC(2,2) = NaN
DE = rand(1,2); DE(1,1) = NaN
FG = rand(3,2); FG(2,2) = NaN
We list the variables in the workspace using
whos
and store this list in
variables
.
variables = who;
We then use a
for
loop to store the content of each variable in
v
using
eval
and then locate the
NaN
s in
v
using
isnan
(Section 2.4) and replace them with
-999
. h e function
eval
executes a MATLAB expression stored in a text string.
We assign the value of
v
to the variable in the base workspace and then clear
the variables
i
,
v
and
variables
, which are no longer needed.