Graphics Programs Reference
In-Depth Information
To get the following result Iused trial and error to get the right values
for the surface reflectance properties:
clf
h = surf(x,y,z);
set(h,'facecolor',[.5 .5 .5],...
'edgecol','none')
hl = light('pos',[1000,0,0]);
axis equal
axis off
view(-20,0)
set(h,'specularstrength',0)
set(h,'ambientstrength',1)
set(h,'diffusestrength',10)
set(h,'backfacelighting','unlit')
38 MATLAB Programming
38.1 Vectorising Code
Ionce heard Cleve Moler say, “The for loop gets a bad rap.” One
of the clearest ways to see the truth of this statement will be in this
section on speeding up matlab routines by vectorising code. We will be
eliminating for loops and replacing them with operations on vectors or
matrices. Yet, do not think that you must then eliminate all for loops
from your matlab code. When for loops are used appropriately they
are still very fast, e@cient, and convenient. With this proviso, let us
look at an example.
matlab's diff function takes the difference between successive pairs
of elements in a vector, and writes them to another vector. Suppose you
want to carry out a similar operation, except now you want to compute
the sum of successive pairs instead of the difference. In mathematical
notation, you would write the formula:
b i = a i + a i +1 ,
i =1 , 2 ,...N
1 .
where a is the input vector of length N , and b is the output vector of
pairwise sums. The following piece of matlab code would do the job:
N = length(a);
b = zeros(1,N - 1);
for i = 1:N-1
b(i) = a(i) + a(i + 1);
end
This code, or at least the line inside the for loop, has the advantage
of resembling the mathematical notation quite closely. We measure the
Search WWH ::




Custom Search