Code covered by the BSD License  

Highlights from
disperse

5.0

5.0 | 3 ratings Rate this file 17 Downloads (last 30 days) File Size: 4.22 KB File ID: #33866
image thumbnail

disperse

by Sam Hallman

 

21 Nov 2011 (Updated 30 Nov 2011)

Assign elements of an input array to individual output variables with a single function call.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information
Description

Oftentimes when using Matlab we find ourselves having to perform a series of repetitive 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 to do this more simply? DISPERSE is syntactic sugar for performing these kinds of tasks with a single function call.

DISPERSE is not the same as DEAL. For example, if A is a vector then [a b c d] = disperse(A) performs a=A(1), b=A(2), c=A(3), and d=A(4), whereas [a b c d] = deal(A) performs a=A, b=A, c=A, and d=A.

DISPERSE generalizes to arbitrary dimensions, and is extended to follow analogous behavior on cell arrays and structure arrays. For example, if A is an RGB image, then [r g b] = disperse(A) grabs the R, G, and B color channels from A. See the documentation for more examples.

---------
Edit: I was recently notified that there is a similar submission called VECT2VAR. For vector inputs, these functions are equivalent; however, DISPERSE also works on higher-dimensional objects, which is handy when dealing with matrices, images, videos, etc.

Required Products MATLAB
MATLAB release MATLAB 7.9 (2009b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (3)
01 Dec 2011 James Hall

Very nice! Works exactly as advertised and will be very useful.

02 Mar 2012 Yuri Kotliarov

Great! Looked for such function.

03 Mar 2012 wenwu xiu

Great job,Sam, thanks, works excellent

Please login to add a comment or rating.
Updates
30 Nov 2011

Edited the description to acknowledge a related submission. Edited the comments to mention the html documentation.

Tag Activity for this File
Tag Applied By Date/Time
convenience Sam Hallman 21 Nov 2011 15:36:47
potw Lindsay Coutinho 02 Mar 2012 11:16:09
pick of the week Lindsay Coutinho 02 Mar 2012 11:16:09

Contact us at files@mathworks.com