Chapter 4: Programming in Matlab - College of the Redwoods
Chapter 4: Programming in Matlab - College of the Redwoods
Chapter 4: Programming in Matlab - College of the Redwoods
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
266 <strong>Chapter</strong> 4 <strong>Programm<strong>in</strong>g</strong> <strong>in</strong> <strong>Matlab</strong><br />
Take a moment to check that <strong>in</strong> each position <strong>in</strong> B that conta<strong>in</strong>s a logical 1 (true),<br />
<strong>the</strong> correspond<strong>in</strong>g position <strong>in</strong> matrix A conta<strong>in</strong>s a uniform random number that<br />
is less than 0.5.<br />
There are a number <strong>of</strong> th<strong>in</strong>gs that we can do with this <strong>in</strong>formation. We could<br />
list all <strong>the</strong> random numbers <strong>in</strong> A that are less than 0.5 by us<strong>in</strong>g matrix B as a<br />
logical <strong>in</strong>dex <strong>in</strong>to <strong>the</strong> matrix A. 2<br />
>> A(B)<br />
ans =<br />
0.4565<br />
0.0185<br />
0.4447<br />
0.1763<br />
0.4057<br />
0.4103<br />
0.0579<br />
0.3529<br />
0.0099<br />
0.1389<br />
0.2028<br />
0.1987<br />
0.2722<br />
0.1988<br />
0.0153<br />
Or we could set each <strong>of</strong> <strong>the</strong>se positions to zero.<br />
>> A(B)=0<br />
A =<br />
0 0.7919 0.9355 0 0<br />
0 0.9218 0.9169 0.8132 0.6038<br />
0.8214 0.7382 0 0 0<br />
0 0 0.8936 0 0<br />
0.6154 0 0 0 0<br />
2 <strong>Matlab</strong> has roots that began <strong>the</strong>ir growth <strong>in</strong> Fortran, where <strong>the</strong> entries <strong>in</strong> arrays were loaded<br />
by column. The <strong>Matlab</strong> command A(:) will list all <strong>the</strong> entries <strong>in</strong> A, first go<strong>in</strong>g down <strong>the</strong><br />
first column, <strong>the</strong>n <strong>the</strong> second, etc. With <strong>the</strong> command A(B), <strong>Matlab</strong> first lists all <strong>the</strong> “true”<br />
positions <strong>in</strong> <strong>the</strong> first column, <strong>the</strong>n <strong>the</strong> second, etc.