15.11.2014 Views

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

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Section 4.1 Logical Arrays 281<br />

>> u=[2,3], v=[4 5 6], u|v<br />

u =<br />

2 3<br />

v =<br />

4 5 6<br />

??? Error us<strong>in</strong>g ==> or<br />

Inputs must have <strong>the</strong> same size.<br />

Let’s look at some powerful applications <strong>of</strong> <strong>the</strong>se operators.<br />

◮ Example 3.<br />

F<strong>in</strong>d all entries <strong>of</strong> matrix magic(5) that are not prime.<br />

Start by enter<strong>in</strong>g <strong>the</strong> matrix and us<strong>in</strong>g <strong>Matlab</strong>’s isprime function to f<strong>in</strong>d <strong>the</strong><br />

prime entries.<br />

>> M=magic(5); B=isprime(M);<br />

We saw <strong>in</strong> an earlier example that M(B) will list all <strong>of</strong> <strong>the</strong> primes <strong>in</strong> <strong>the</strong> matrix<br />

M. So, let’s use <strong>the</strong> logical operator ~ to list all <strong>the</strong> nonprimes. For convenience<br />

and purposes <strong>of</strong> comparison, we list <strong>the</strong> matrix M.<br />

>> M<br />

M =<br />

17 24 1 8 15<br />

23 5 7 14 16<br />

4 6 13 20 22<br />

10 12 19 21 3<br />

11 18 25 2 9<br />

Now we list all <strong>the</strong> nonprimes <strong>in</strong> <strong>the</strong> matrix M.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!