How to separate a matrix by zeros?
14 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Koos Toebes
il 5 Giu 2016
Commentato: Image Analyst
il 18 Mag 2021
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 Commenti
Mollymomo
il 18 Ott 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
il 18 Ott 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).
Risposta accettata
Weird Rando
il 5 Giu 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 Commenti
Wirattawut Boonbandansook
il 17 Mag 2021
Modificato: Wirattawut Boonbandansook
il 17 Mag 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
il 18 Mag 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?
Più risposte (1)
Image Analyst
il 5 Giu 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)
Vedere anche
Categorie
Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!