How to separate a matrix by zeros?

9 次查看(过去 30 天)
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 个评论
Mollymomo
Mollymomo 2018-10-18
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 2018-10-18
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).

请先登录,再进行评论。

采纳的回答

Weird Rando
Weird Rando 2016-6-5
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 个评论
Wirattawut Boonbandansook
编辑:Wirattawut Boonbandansook 2021-5-17
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 2021-5-18
@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?

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2016-6-5
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)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by