How to return the sequential number of nonzero and zero elements in a list?

Given list A = [1, 2, 3, 4, 5, 0, 0, 3, 4, 5, 0, 0, 0, 0], how can I return a matrix of the sequential number of non-zero and zero elements in the list? In the above example, the output should be B = [5, 2; 3, 4].

댓글 수: 1

And what would you want for this:
A = [0, 0, 1, 2, 3, 0, 0, 3, 4, 5, 0, 0, 0]
or would you never have a different number of zero and non-zero stretches?

댓글을 달려면 로그인하십시오.

 채택된 답변

B = reshape(diff([find([1;diff(A(:)>0)~=0]);numel(A)+1]),2,[])';

추가 답변 (1개)

Sounds a lot like homework. Here's a hint:
A = [1, 2, 3, 4, 5, 0, 0, 3, 4, 5, 0, 0, 0, 0]
% Find lengths of non-zero stretches of data.
labeled = bwlabel(A ~= 0)
measurements = regionprops(labeled, 'Area', 'PixelList');
allAreas1 = [measurements.Area] % Produces 5 3
% Find lengths of zero stretches of data.
labeled = bwlabel(A == 0)
measurements = regionprops(labeled, 'Area', 'PixelList');
allAreas0 = [measurements.Area] % Produces 2 4
% Interleave allAreas1 and allAreas0 to form "B"
% See if you can do this part yourself.

카테고리

도움말 센터File Exchange에서 Deep Learning Toolbox에 대해 자세히 알아보기

질문:

2015년 10월 23일

댓글:

2015년 10월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by