MATLAB: Return all possible Cuts of array A If you have defined number of parts M

array partitioning

Lets say you have input Array A with with input length N and you have also input number M which defines how many array cuts you need to have. The goal is to return all possible cuts.
N/M <= 1
So for example if you have Array A = [1,2,3,4] and M = 1
solution is array L containing arrays
[ [1] [2,3,4] ]
[ [1,2] [3,4] ]
[ [1,2,3] [4] ]
empty partitions are not allowed

Best Answer

"empty partitions are allowed"
I assume you mean not allowed, otherwise your solution is missing two cuts.
%V: a row vector to cut
%ncuts: the number of cuts
cutposition = nchoosek(1:numel(V)-1, ncuts-1); %each row is the index after which to perform a cut, for a particular cut

cuts = cell(size(cutposition, 1), 1);
for cut = 1:size(cutposition, 1) %iterate over all the possible cuts

cuts{cut} = mat2cell(V, 1, diff([0, cutposition(cut, :), numel(V)])); %actual cutting

end
--edit:--
If you want you can have cuts as a 2D cell array instead of a cell array of cell arrays:
cutposition = nchoosek(1:numel(V)-1, ncuts-1); %each row is the index after which to perform a cut, for a particular cut
cuts = cell(size(cutposition, 1), ncuts);
for cut = 1:size(cutposition, 1) %iterate over all the possible cuts
cuts(cut, :) = mat2cell(V, 1, diff([0, cutposition(cut, :), numel(V)])); %actual cutting
end