File Exchange

image thumbnail

Reverse concatenation: separate a data array along a specified dimension.

version (1.62 KB) by DS
INVERSE_CAT splits a given data array into sub-arrays along the specified dimension.

1 Download

Updated 10 Apr 2012

View License

[A B]=INVERSE_CAT(DIM,C) splits array C along dimension, DIM, returning sub-arrays A and B.

M = [1 2 3; 4 5 6; 7 8 9];
C = cat(2,M,M)
[A B] = inverse_cat(2,C) ... returns A=M and B=M
[A B] = inverse_cat(1,rot90(C)) ... returns A=rot90(M) B=rot90(M)
[A B] = inverse_cat(3,cat(3,M,M)) ... returns A=M and B=M

Note: There are certainly more efficient ways to separate concatenated data. Suggestions for improvement are much appreciated.

*Thanks to Jan Simon for insightful comments.

Cite As

DS (2020). Reverse concatenation: separate a data array along a specified dimension. (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (3)

André Sprengers

Bryant Svedin

This works great. I made a small change for my own way of wanting to use this. It now just gives the output as a 1xN cell, where N is the length of the dimension I just did inverse_cat on.


"x{i} = 1:5" is faster than "x(i) = {1:5}".
It would look nicer and save some micro-seconds to compute "floor(size(C,DIM)/nout)" once only and store it in a variable.
"ndims(C)" is nicer than "numel(size(C))".
The VARARGIN in the inputs is not used, so I suggest to omit it.
The first element does not need an exceptional handling, therefore I'd include it in the loops to simplify the code:
q = floor(size(C,DIM)/nout);
for i = 1:nout
x{i} = C(i*q - q + 1:i*q, :);
A method for indexing along any dimension:
Index = cell(1, ndims(C));
Index(:) = {':'};
for i = 1:nout
Index{DIM} = i*q - q + 1:i*q;
x{i} = C(Index{:});
But this is slower than the explicite method.


Added Jan Simon's method to handle dimensions > 3.

MATLAB Release Compatibility
Created with R2007b
Compatible with any release
Platform Compatibility
Windows macOS Linux