Geoscience Reference
In-Depth Information
comparisons between the variables (or numbers) to the let and right of the
equal signs and returns an array of the same size, made up of elements set to
logical 1 where the relationship is true and to logical 0 where it is not true. In
our example m==1 returns 1 if m equals 1 and 0 if m equals any other value. h e
last line of the if-then construct computes the average by dividing the sum
of elements by m or n . We do not use a semicolon here in order to allow the
output of the result. We can now save our new M-i le as average.m and type
clear
x = [3 6 2 -3 8];
in the Command Window to dei ne an example array x . We then type
average
without the extension .m to run our script and obtain the average of the
elements of the array x as output.
ans =
3.2000
At er typing
whos
we see that the workspace now contains
Name Size Bytes Class Attributes
ans 1x1 8 double
m 1x1 8 double
n 1x1 8 double
x 1x5 40 double
h e listed variables are the example array x and the output of the function
size , m and n . h e result of the operation is stored in the variable ans . Since
the default variable ans might be overwritten during one of the succeeding
operations, we need to dei ne a dif erent variable. Typing
a = average
however, results in the error message
??? Attempt to execute SCRIPT average as a function.
We can obviously not assign a variable to the output of a script. Moreover,
all variables dei ned and used in the script appear in the workspace; in
our example these are the variables m and n . Scripts contain sequences of
Search WWH ::




Custom Search