How to separate a matrix by zeros?

14 views (last 30 days)
Hi guys. For an assignment, I have to split up a matrix, separated by zeros. For just a vector it worked like this:
A = [1 2 3 0 0 0 0 0 2 3 4 0 0 0 0 0 4 5 6 7 0 0 0 0 1 1 1];
ne0 = find(A~=0); % Nonzero Elements
ix0 = unique([ne0(1) ne0(diff([0 ne0])>1)]); % Segment Start Indices
ix1 = ne0([find(diff([0 ne0])>1)-1 length(ne0)]); % Segment End Indices
for k1 = 1:length(ix0)
section{k1} = A(ix0(k1):ix1(k1));
end
celldisp(section) % Display Results
I found the above script somewhere else here.
But now I have to do more or less the same for a matrix. For example:
2 9
3 5
7 2
0 4
0 2
0 7
1 4
8 5
0 4
2 8
into:
2 9
3 5
7 2
1 4
8 5
2 8
I tried to modify the script above, but that was not successful.
  2 Comments
Mollymomo
Mollymomo on 18 Oct 2018
Is there a way I can use the original one for a matrix? when I try to use it for a matrix it gives me an error about horzcat that says "Dimensions of arrays being concatenated are not consistent."
Image Analyst
Image Analyst on 18 Oct 2018
To concatenate vertically the number of columns of the matrices must match. To concatenate horizontally the number of rows of the matrices must match. Otherwise, how could you stitch them together? If you still have a problem read this link and post in a new question (not here).

Sign in to comment.

Accepted Answer

Weird Rando
Weird Rando on 5 Jun 2016
ne0 = find(A(:,1)~=0)'; % Nonzero Elements (transposed)
ix0 = unique([ne0(1) ne0(diff([0 ne0])>1)]); % Segment Start Indices
ix1 = ne0([find(diff([0 ne0])>1)-1 length(ne0)]); % Segment End Indices
for k1 = 1:length(ix0)
section{k1} = A(ix0(k1):ix1(k1),:); % (Included the column)
end
celldisp(section) % Display Results
  3 Comments
Wirattawut Boonbandansook
Wirattawut Boonbandansook on 17 May 2021
Edited: Wirattawut Boonbandansook on 17 May 2021
I tried running the code above but it errored out at 'ix1' with the error 'Array indices must be positive integers or logical values.' May you clarify your logic in that line please?
Image Analyst
Image Analyst on 18 May 2021
@Wirattawut Boonbandansook, I just ran it and it ran fine:
A = [1 2 3 0 0 0 0 0 2 3 4 0 0 0 0 0 4 5 6 7 0 0 0 0 1 1 1];
ne0 = find(A(:,1)~=0)'; % Nonzero Elements (transposed)
ix0 = unique([ne0(1) ne0(diff([0 ne0])>1)]); % Segment Start Indices
ix1 = ne0([find(diff([0 ne0])>1)-1 length(ne0)]); % Segment End Indices
for k1 = 1:length(ix0)
section{k1} = A(ix0(k1):ix1(k1),:); % (Included the column)
end
celldisp(section) % Display Results
section{1} =
Columns 1 through 20
1 2 3 0 0 0 0 0 2 3 4 0 0 0 0 0 4 5 6 7
Columns 21 through 27
0 0 0 0 1 1 1
What did you do to change it?

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 5 Jun 2016
An alternate way, if you have the Image Processing Toolbox is
m = [...
2 9
3 5
7 2
0 4
0 2
0 7
1 4
8 5
0 4
2 8]
% If there is a zero in column 1, make the zero in column 2 also
m(m(:,1)==0, 2) = 0
[labeledMatrix, numberOfRegions] = bwlabel(m) % Identify separate regions
% Extract/crop all the separate sub-arrays from m and save in a cell array
for k = 1 : numberOfRegions
% Get the values
theRegions{k} = m(labeledMatrix(:, 1) == k, :);
end
% Print out all the regions to the command window.
celldisp(theRegions)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!