File Exchange

image thumbnail


version 1.2 (4.22 KB) by

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



View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

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.

Comments and Ratings (5)

Sky Sartorius

Sky Sartorius (view profile)

I keep finding more uses for this. Thanks for the submission.

Michael Völker

Very nice, simplistic code.

However, if the input is a complex column vector, you accidentally conjugate it, due to this line:
x = x';

Could you please repair that?
--> x = x.';

wenwu xiu

Great job,Sam, thanks, works excellent

Yuri K

Yuri K (view profile)

Great! Looked for such function.

James Hall

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



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

MATLAB Release
MATLAB 7.9 (R2009b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video