How to separate a matrix by zeros?
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Koos Toebes
el 5 de Jun. de 2016
Comentada: Image Analyst
el 18 de Mayo de 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 comentarios
Mollymomo
el 18 de Oct. de 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
el 18 de Oct. de 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).
Respuesta aceptada
Weird Rando
el 5 de Jun. de 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 comentarios
Wirattawut Boonbandansook
el 17 de Mayo de 2021
Editada: Wirattawut Boonbandansook
el 17 de Mayo de 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
el 18 de Mayo de 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?
Más respuestas (1)
Image Analyst
el 5 de Jun. de 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)
Ver también
Categorías
Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!