How to separate a matrix by zeros?
14 ビュー (過去 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
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
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
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
2021 年 5 月 17 日
編集済み: 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
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
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 Center および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!