Split matrix elements into separate variables.
Updated 13 Nov 2014

View License

This function takes an array as input and returns each element of the array as a separate output. It is similar in function to deal, but less verbose when dealing with a single matrix.

I created this function because I wanted a one-liner alternative to the following code:
c = num2cell(rect); %rect is a rectangle position vector
[x,y,wid,hei] = c{:};

Now I can simply use:
[x,y,wid,hei] = matsplit(rect);

The number of output arguments does not need to match the number of elements in the input array.

Alternatively, you can split the matrix by row or column using the second input DIM.

A = [1 2 3 4
1 2 3 4
1 2 3 4];
[B,C] = matsplit(A,1);


B = [1;1;1];
C = [2;2;2];

Cite As

Matthew Eicholtz (2024). matsplit (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2014a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Numeric Types in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes

Added image.

Removed requirement that nargout==numel(A).
Added second (optional) input to split matrix by row or column.

Minor changes to description.