Counting zeros which are lying between nonzero elements by considering consecutive zeros as a single element in a matrix

1 回表示 (過去 30 日間)
I have a (4x8) matrix as
A=[1 0 1 1 1 0 0 1;0 1 0 1 1 0 1 0;0 0 1 0 0 0 0 0;1 0 1 1 0 0 1 1]
A = 4×8
1 0 1 1 1 0 0 1 0 1 0 1 1 0 1 0 0 0 1 0 0 0 0 0 1 0 1 1 0 0 1 1
I want to calculate the number of only those zeros that are lying between nonzero elements i.e 1 in each row in such a way that I want to consider only the consecutive zeros as a single element. Single zeros will be considered as a separate element. Desired Output is (2 2 0 2). How to do so?

回答 (5 件)

Mahdi Hayati
Mahdi Hayati 2023 年 8 月 27 日
Hi.
you can use diff() function to find number of times that elements of each row, turn from 1 to 0. for example for the first row we can have:
D = diff(A(1,:));
temp = size(find(D == -1));
number_of_turns = temp(1);
if D(7) == -1
number_of_turns = number_of_turns - 1;
end
number_of_turns
in this code, I found how many times zeros are stuck between ones. The 'if' statement is because if the last element of the row is 0, it must not be count.
I hope it was useful

Bruno Luong
Bruno Luong 2023 年 8 月 27 日
編集済み: Bruno Luong 2023 年 8 月 27 日
A=[1 0 1 1 1 0 0 1;0 1 0 1 1 0 1 0;0 0 1 0 0 0 0 0;1 0 1 1 0 0 1 1]
A = 4×8
1 0 1 1 1 0 0 1 0 1 0 1 1 0 1 0 0 0 1 0 0 0 0 0 1 0 1 1 0 0 1 1
d=diff(A~=0,1,2);
[~,i,v]=find(d');
vl=zeros(size(A,1),1);
vl(i)=v;
sum(d==-1,2)-(vl==-1)
ans = 4×1
2 2 0 2

Bruno Luong
Bruno Luong 2023 年 8 月 27 日
Always helpful the old for-loop
A=[1 0 1 1 1 0 0 1;0 1 0 1 1 0 1 0;0 0 1 0 0 0 0 0;1 0 1 1 0 0 1 1]
A = 4×8
1 0 1 1 1 0 0 1 0 1 0 1 1 0 1 0 0 0 1 0 0 0 0 0 1 0 1 1 0 0 1 1
[m,n] = size(A);
count = zeros(m,1);
for i = 1:m
s0startded = false;
c = 0;
isprevnull = A(i,1) == 0;
for j = 2:n
isnull = A(i,j) == 0;
if s0startded
if ~isnull
c = c+1;
s0startded = false;
end
else
s0startded = isnull && ~isprevnull;
end
isprevnull = isnull;
end
count(i) = c;
end
count
count = 4×1
2 2 0 2

Matt J
Matt J 2023 年 8 月 27 日
編集済み: Matt J 2023 年 8 月 27 日
A=[1 0 1 1 1 0 0 1;0 1 0 1 1 0 1 0;0 0 1 0 0 0 0 0;1 0 1 1 0 0 1 1]
A = 4×8
1 0 1 1 1 0 0 1 0 1 0 1 1 0 1 0 0 0 1 0 0 0 0 0 1 0 1 1 0 0 1 1
d=diff(A,1,2);
result= max(0, min( sum(d==-1,2) - ~A(:,end), sum(d==1,2) - ~A(:,1) ))
result = 4×1
2 2 0 2
  4 件のコメント
Bruno Luong
Bruno Luong 2023 年 8 月 27 日
編集済み: Bruno Luong 2023 年 8 月 27 日
It seems not correct with A contains only 0
A = [0 0 0 0]
A = 1×4
0 0 0 0
d=diff(A,1,2);
result= min( sum(d==-1,2) - ~A(:,end), sum(d==1,2) - ~A(:,1) )
result = -1
Matt J
Matt J 2023 年 8 月 27 日
Yeah. I added a threshold step.

サインインしてコメントする。


Bruno Luong
Bruno Luong 2023 年 8 月 28 日
編集済み: Bruno Luong 2023 年 8 月 28 日
A=[1 0 1 1 1 0 0 1;0 1 0 1 1 0 1 0;0 0 1 0 0 0 0 0;1 0 1 1 0 0 1 1]
A = 4×8
1 0 1 1 1 0 0 1 0 1 0 1 1 0 1 0 0 0 1 0 0 0 0 0 1 0 1 1 0 0 1 1
max(sum(diff(~~A,1,2)==1,2)-~A(:,1),0)
ans = 4×1
2 2 0 2
% If A is binary you can simplify to
% max(sum(diff(A,1,2)==1,2)-~A(:,1),0)

カテゴリ

Help Center および File ExchangeSpecifying Target for Graphics Output についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by