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
Image Analyst
2015년 10월 23일
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?
채택된 답변
추가 답변 (1개)
Image Analyst
2015년 10월 23일
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에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!