, -, , :
v = reshape(padarray(a, [1 0]), [], 1);
% Or if you don't have the Image Processing Toolbox function padarray...
v = reshape([zeros(1, size(a, 2)); a; zeros(1, size(a, 2))], [], 1);
, 1 , :
endPoints = find(diff(v) ~= 0); % Find where transitions to or from 0 occur
spans = endPoints(2:2:end)-endPoints(1:2:end); % Index of transitions to 0 minus
% index of transitions from 0
, , , :
b = accumarray(v(endPoints(1:2:end)+1), spans, [], @(v) {v(:).'}).';
:
b =
1×3 cell array
[5] [1×2 double] [3]
:
spans (.. b{2} [3 4] [4 3]). , . b:
[vals, index] = sort(v(endPoints(1:2:end)+1));
b = accumarray(vals, spans(index), [], @(v) {v(:).'}).';