1 | |
---|
2 | <!DOCTYPE html |
---|
3 | PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
---|
4 | <html> |
---|
5 | <head> |
---|
6 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
---|
7 | |
---|
8 | <!-- |
---|
9 | This HTML is auto-generated from an M-file. |
---|
10 | To make changes, update the M-file and republish this document. |
---|
11 | --> |
---|
12 | <title>Getting Started</title> |
---|
13 | <meta name="generator" content="MATLAB 7.8"> |
---|
14 | <meta name="date" content="2011-11-18"> |
---|
15 | <meta name="m-file" content="sam_demo"><style type="text/css"> |
---|
16 | |
---|
17 | body { |
---|
18 | background-color: white; |
---|
19 | margin:10px; |
---|
20 | } |
---|
21 | |
---|
22 | h1 { |
---|
23 | color: #990000; |
---|
24 | font-size: x-large; |
---|
25 | } |
---|
26 | |
---|
27 | h2 { |
---|
28 | color: #990000; |
---|
29 | font-size: medium; |
---|
30 | } |
---|
31 | |
---|
32 | /* Make the text shrink to fit narrow windows, but not stretch too far in |
---|
33 | wide windows. */ |
---|
34 | p,h1,h2,div.content div { |
---|
35 | max-width: 600px; |
---|
36 | /* Hack for IE6 */ |
---|
37 | width: auto !important; width: 600px; |
---|
38 | } |
---|
39 | |
---|
40 | pre.codeinput { |
---|
41 | background: #EEEEEE; |
---|
42 | padding: 10px; |
---|
43 | } |
---|
44 | @media print { |
---|
45 | pre.codeinput {word-wrap:break-word; width:100%;} |
---|
46 | } |
---|
47 | |
---|
48 | span.keyword {color: #0000FF} |
---|
49 | span.comment {color: #228B22} |
---|
50 | span.string {color: #A020F0} |
---|
51 | span.untermstring {color: #B20000} |
---|
52 | span.syscmd {color: #B28C00} |
---|
53 | |
---|
54 | pre.codeoutput { |
---|
55 | color: #666666; |
---|
56 | padding: 10px; |
---|
57 | } |
---|
58 | |
---|
59 | pre.error { |
---|
60 | color: red; |
---|
61 | } |
---|
62 | |
---|
63 | p.footer { |
---|
64 | text-align: right; |
---|
65 | font-size: xx-small; |
---|
66 | font-weight: lighter; |
---|
67 | font-style: italic; |
---|
68 | color: gray; |
---|
69 | } |
---|
70 | |
---|
71 | </style></head> |
---|
72 | <body> |
---|
73 | <div class="content"> |
---|
74 | <h1>disperse.m</h1> |
---|
75 | <!--introduction--> |
---|
76 | <p>Sam Hallman</p><pre> code written: 26 May 2010 |
---|
77 | html written: 18 Nov 2011</pre><p>Demonstration of the DISPERSE function. Oftentimes when using Matlab we find ourselves having to perform a series of repetitive |
---|
78 | assignments from some kind of array, e.g., a=A(1); b=A(2); c=A(3); . . . etc. Inevitably we ask ourselves, is there a way |
---|
79 | to do this more simply? DISPERSE is syntactic sugar for performing these kinds of tasks with a single function call. |
---|
80 | </p> |
---|
81 | <p>The following examples illustrate the behavior of DISPERSE on different collection classes.</p> |
---|
82 | <!--/introduction--> |
---|
83 | <h2>Contents</h2> |
---|
84 | <div> |
---|
85 | <ul> |
---|
86 | <li><a href="#1">Vectors</a></li> |
---|
87 | <li><a href="#3">Matrices</a></li> |
---|
88 | <li><a href="#5">N-D arrays</a></li> |
---|
89 | <li><a href="#7">RGB images (special case)</a></li> |
---|
90 | <li><a href="#9">Cells</a></li> |
---|
91 | <li><a href="#11">Structs</a></li> |
---|
92 | </ul> |
---|
93 | </div> |
---|
94 | <h2>Vectors<a name="1"></a></h2> |
---|
95 | <p>Grab elements of vectors</p><pre class="codeinput">foo = rand(1,4) |
---|
96 | [a b c d] = disperse(foo) |
---|
97 | </pre><pre class="codeoutput"> |
---|
98 | foo = |
---|
99 | |
---|
100 | 0.4094 0.3654 0.8578 0.9957 |
---|
101 | |
---|
102 | |
---|
103 | a = |
---|
104 | |
---|
105 | 0.4094 |
---|
106 | |
---|
107 | |
---|
108 | b = |
---|
109 | |
---|
110 | 0.3654 |
---|
111 | |
---|
112 | |
---|
113 | c = |
---|
114 | |
---|
115 | 0.8578 |
---|
116 | |
---|
117 | |
---|
118 | d = |
---|
119 | |
---|
120 | 0.9957 |
---|
121 | |
---|
122 | </pre><p>Can grab a subset if you like</p><pre class="codeinput">v = rand(1,5) |
---|
123 | [a b c] = disperse(v([1 4 5])) |
---|
124 | </pre><pre class="codeoutput"> |
---|
125 | v = |
---|
126 | |
---|
127 | 0.9096 0.7282 0.6020 0.0011 0.2741 |
---|
128 | |
---|
129 | |
---|
130 | a = |
---|
131 | |
---|
132 | 0.9096 |
---|
133 | |
---|
134 | |
---|
135 | b = |
---|
136 | |
---|
137 | 0.0011 |
---|
138 | |
---|
139 | |
---|
140 | c = |
---|
141 | |
---|
142 | 0.2741 |
---|
143 | |
---|
144 | </pre><h2>Matrices<a name="3"></a></h2> |
---|
145 | <p>Grab columns of a matrix</p><pre class="codeinput">A = rand(2) |
---|
146 | [c1 c2] = disperse(A) |
---|
147 | </pre><pre class="codeoutput"> |
---|
148 | A = |
---|
149 | |
---|
150 | 0.3656 0.2508 |
---|
151 | 0.2083 0.7516 |
---|
152 | |
---|
153 | |
---|
154 | c1 = |
---|
155 | |
---|
156 | 0.3656 |
---|
157 | 0.2083 |
---|
158 | |
---|
159 | |
---|
160 | c2 = |
---|
161 | |
---|
162 | 0.2508 |
---|
163 | 0.7516 |
---|
164 | |
---|
165 | </pre><p>Transpose if you want rows</p><pre class="codeinput">[r1 r2] = disperse(A') |
---|
166 | </pre><pre class="codeoutput"> |
---|
167 | r1 = |
---|
168 | |
---|
169 | 0.3656 |
---|
170 | 0.2508 |
---|
171 | |
---|
172 | |
---|
173 | r2 = |
---|
174 | |
---|
175 | 0.2083 |
---|
176 | 0.7516 |
---|
177 | |
---|
178 | </pre><h2>N-D arrays<a name="5"></a></h2> |
---|
179 | <p>disperse generalizes to arrays of arbitrary dimension</p><pre class="codeinput">A = rand(5,3,4,9,7); |
---|
180 | <span class="comment">% pick off A(:,:,:,:,1) and A(:,:,:,:,2)</span> |
---|
181 | [a b] = disperse(A); |
---|
182 | </pre><p>it worked:</p><pre class="codeinput">whos <span class="string">A</span> <span class="string">a</span> <span class="string">b</span> |
---|
183 | isequal(a, A(:,:,:,:,1)) |
---|
184 | isequal(b, A(:,:,:,:,2)) |
---|
185 | </pre><pre class="codeoutput"> Name Size Bytes Class Attributes |
---|
186 | |
---|
187 | A 5-D 30240 double |
---|
188 | a 4-D 4320 double |
---|
189 | b 4-D 4320 double |
---|
190 | |
---|
191 | |
---|
192 | ans = |
---|
193 | |
---|
194 | 1 |
---|
195 | |
---|
196 | |
---|
197 | ans = |
---|
198 | |
---|
199 | 1 |
---|
200 | |
---|
201 | </pre><h2>RGB images (special case)<a name="7"></a></h2> |
---|
202 | <p>The techniques of the previous section are especially handy for RGB images:</p><pre class="codeinput"><span class="comment">% Grab the R, G, and B color channels</span> |
---|
203 | im = imread(<span class="string">'street1.jpg'</span>); |
---|
204 | [r g b] = disperse(im); |
---|
205 | </pre><p>it worked:</p><pre class="codeinput">whos <span class="string">im</span> <span class="string">r</span> <span class="string">g</span> <span class="string">b</span> |
---|
206 | isequal(r, im(:,:,1)) |
---|
207 | isequal(g, im(:,:,2)) |
---|
208 | isequal(b, im(:,:,3)) |
---|
209 | </pre><pre class="codeoutput"> Name Size Bytes Class Attributes |
---|
210 | |
---|
211 | b 480x640 307200 uint8 |
---|
212 | g 480x640 307200 uint8 |
---|
213 | im 480x640x3 921600 uint8 |
---|
214 | r 480x640 307200 uint8 |
---|
215 | |
---|
216 | |
---|
217 | ans = |
---|
218 | |
---|
219 | 1 |
---|
220 | |
---|
221 | |
---|
222 | ans = |
---|
223 | |
---|
224 | 1 |
---|
225 | |
---|
226 | |
---|
227 | ans = |
---|
228 | |
---|
229 | 1 |
---|
230 | |
---|
231 | </pre><h2>Cells<a name="9"></a></h2> |
---|
232 | <p>Grab elements of cells</p><pre class="codeinput">c = { <span class="string">'sam'</span>, -1, rand(5) } |
---|
233 | [c1 c2 c3] = disperse(c) |
---|
234 | </pre><pre class="codeoutput"> |
---|
235 | c = |
---|
236 | |
---|
237 | 'sam' [-1] [5x5 double] |
---|
238 | |
---|
239 | |
---|
240 | c1 = |
---|
241 | |
---|
242 | sam |
---|
243 | |
---|
244 | |
---|
245 | c2 = |
---|
246 | |
---|
247 | -1 |
---|
248 | |
---|
249 | |
---|
250 | c3 = |
---|
251 | |
---|
252 | 0.2945 0.0056 0.9591 0.9123 0.1125 |
---|
253 | 0.6334 0.0572 0.8658 0.1883 0.6698 |
---|
254 | 0.5971 0.0029 0.2624 0.4064 0.2496 |
---|
255 | 0.7556 0.5436 0.4880 0.2552 0.6699 |
---|
256 | 0.6972 0.7148 0.4085 0.7605 0.3863 |
---|
257 | |
---|
258 | </pre><p>You can go further</p><pre class="codeinput">c = { <span class="string">'foo'</span> {} ; [1,2;3,4] <span class="string">'bar'</span> } |
---|
259 | [a b] = disperse(c) |
---|
260 | </pre><pre class="codeoutput"> |
---|
261 | c = |
---|
262 | |
---|
263 | 'foo' {} |
---|
264 | [2x2 double] 'bar' |
---|
265 | |
---|
266 | |
---|
267 | a = |
---|
268 | |
---|
269 | 'foo' |
---|
270 | [2x2 double] |
---|
271 | |
---|
272 | |
---|
273 | b = |
---|
274 | |
---|
275 | {} |
---|
276 | 'bar' |
---|
277 | |
---|
278 | </pre><h2>Structs<a name="11"></a></h2> |
---|
279 | <p>Grab elements of structure arrays</p><pre class="codeinput">s = struct(<span class="string">'strings'</span>, {<span class="string">'sam'</span>,<span class="string">'hello'</span>}, <span class="string">'lengths'</span>, [3 5]) |
---|
280 | [a b] = disperse(s) |
---|
281 | </pre><pre class="codeoutput"> |
---|
282 | s = |
---|
283 | |
---|
284 | 1x2 struct array with fields: |
---|
285 | strings |
---|
286 | lengths |
---|
287 | |
---|
288 | |
---|
289 | a = |
---|
290 | |
---|
291 | strings: 'sam' |
---|
292 | lengths: [3 5] |
---|
293 | |
---|
294 | |
---|
295 | b = |
---|
296 | |
---|
297 | strings: 'hello' |
---|
298 | lengths: [3 5] |
---|
299 | |
---|
300 | </pre><p class="footer"><br> |
---|
301 | Published with MATLAB® 7.8<br></p> |
---|
302 | </div> |
---|
303 | <!-- |
---|
304 | ##### SOURCE BEGIN ##### |
---|
305 | %% disperse.m |
---|
306 | % Sam Hallman |
---|
307 | % |
---|
308 | % code written: 26 May 2010 |
---|
309 | % html written: 18 Nov 2011 |
---|
310 | % |
---|
311 | % Demonstration of the DISPERSE function. Oftentimes when using Matlab we |
---|
312 | % find ourselves having to perform a series of repetitive assignments from |
---|
313 | % some kind of array, e.g., a=A(1); b=A(2); c=A(3); . . . etc. Inevitably |
---|
314 | % we ask ourselves, is there a way to do this more simply? DISPERSE is |
---|
315 | % syntactic sugar for performing these kinds of tasks with a single |
---|
316 | % function call. |
---|
317 | % |
---|
318 | % The following examples illustrate the behavior of DISPERSE on different |
---|
319 | % collection classes. |
---|
320 | |
---|
321 | |
---|
322 | %% Vectors |
---|
323 | % Grab elements of vectors |
---|
324 | foo = rand(1,4) |
---|
325 | [a b c d] = disperse(foo) |
---|
326 | |
---|
327 | %% |
---|
328 | % Can grab a subset if you like |
---|
329 | v = rand(1,5) |
---|
330 | [a b c] = disperse(v([1 4 5])) |
---|
331 | |
---|
332 | %% Matrices |
---|
333 | % Grab columns of a matrix |
---|
334 | A = rand(2) |
---|
335 | [c1 c2] = disperse(A) |
---|
336 | |
---|
337 | %% |
---|
338 | % Transpose if you want rows |
---|
339 | [r1 r2] = disperse(A') |
---|
340 | |
---|
341 | %% N-D arrays |
---|
342 | % disperse generalizes to arrays of arbitrary dimension |
---|
343 | A = rand(5,3,4,9,7); |
---|
344 | % pick off A(:,:,:,:,1) and A(:,:,:,:,2) |
---|
345 | [a b] = disperse(A); |
---|
346 | |
---|
347 | %% |
---|
348 | % it worked: |
---|
349 | whos A a b |
---|
350 | isequal(a, A(:,:,:,:,1)) |
---|
351 | isequal(b, A(:,:,:,:,2)) |
---|
352 | |
---|
353 | |
---|
354 | %% RGB images (special case) |
---|
355 | % The techniques of the previous section are especially handy for RGB |
---|
356 | % images: |
---|
357 | |
---|
358 | % Grab the R, G, and B color channels |
---|
359 | im = imread('street1.jpg'); |
---|
360 | [r g b] = disperse(im); |
---|
361 | |
---|
362 | %% |
---|
363 | % it worked: |
---|
364 | whos im r g b |
---|
365 | isequal(r, im(:,:,1)) |
---|
366 | isequal(g, im(:,:,2)) |
---|
367 | isequal(b, im(:,:,3)) |
---|
368 | |
---|
369 | %% Cells |
---|
370 | % Grab elements of cells |
---|
371 | |
---|
372 | c = { 'sam', -1, rand(5) } |
---|
373 | [c1 c2 c3] = disperse(c) |
---|
374 | |
---|
375 | %% |
---|
376 | % You can go further |
---|
377 | |
---|
378 | c = { 'foo' {} ; [1,2;3,4] 'bar' } |
---|
379 | [a b] = disperse(c) |
---|
380 | |
---|
381 | %% Structs |
---|
382 | % Grab elements of structure arrays |
---|
383 | |
---|
384 | s = struct('strings', {'sam','hello'}, 'lengths', [3 5]) |
---|
385 | [a b] = disperse(s) |
---|
386 | ##### SOURCE END ##### |
---|
387 | --> |
---|
388 | </body> |
---|
389 | </html> |
---|