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.

1 Data Structures<br />

16<br />

5<br />

9<br />

4<br />

.<br />

.<br />

.<br />

12<br />

1<br />

Single-Colon Indexing with Different Array Types. When you index<br />

into a standard <strong>MATLAB</strong> array using a single colon, <strong>MATLAB</strong> returns a<br />

column vector (see variable n, below). When you index into a structure or cell<br />

array using a single colon, you get a comma-separated list “Comma-Separated<br />

Lists” on page 3-80 (see variables c and s, below).<br />

Create three types of arrays:<br />

n = [1 2 3; 4 5 6];<br />

c = {1 2; 3 4};<br />

s = cell2struct(c, {'a', 'b'}, 1);<br />

s(:,2)=s(:,1);<br />

Use single-colon indexing on each:<br />

n(:) c{:} s(:).a<br />

ans = ans = ans =<br />

1 1 1<br />

4 ans = ans =<br />

2 3 2<br />

5 ans = ans =<br />

3 2 1<br />

6 ans = ans =<br />

4 2<br />

Using a Matrix As an Index<br />

You can repeatedly access an array element using the ones function. To create<br />

a new 2-by-6 matrix out of the ninth element of a 4-by-4 magic square A,<br />

B = A(9 * ones(2, 6))<br />

B =<br />

1-22

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

Saved successfully!

Ooh no, something went wrong!