23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Resizing and Reshaping Matrices<br />

Expanding a Character Array. You can expand character arrays in the<br />

same manner as other <strong>MATLAB</strong> arrays, but it is generally not recommended.<br />

<strong>MATLAB</strong> expands any array by padding uninitialized elements with zeros.<br />

Because zero is interpreted by <strong>MATLAB</strong> and some other programming<br />

languages as a string terminator, you may find that some functions treat the<br />

expanded string as if it were less than its full length.<br />

Expand a 1-by-5 character array to twelve characters. The result appears<br />

atfirsttobeatypicalstring:<br />

greeting = 'Hello';<br />

greeting =<br />

Hello World<br />

greeting(1,8:12) = 'World'<br />

Closer inspection however reveals string terminatorsatthepointofexpansion:<br />

uint8(greeting)<br />

ans =<br />

72 101 108 108 111 0 0 87 111 114 108 100<br />

This causes some functions, like strcmp, to return what might be considered<br />

an unexpected result:<br />

strcmp(greeting, 'Hello<br />

ans =<br />

0<br />

World')<br />

Diminishing the Size of a Matrix<br />

Youcandeleterowsandcolumnsfromamatrixbyassigningtheemptyarray<br />

[] to those rows or columns. Start with<br />

A = magic(4)<br />

A =<br />

16 2 3 13<br />

5 11 10 8<br />

9 7 6 12<br />

4 14 15 1<br />

Then, delete the second column of A using<br />

A(:, 2) = []<br />

1-31

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

Saved successfully!

Ooh no, something went wrong!