How to count alternating ones and zeros in a matrix

7 ビュー (過去 30 日間)
Awais Saeed
Awais Saeed 2021 年 7 月 14 日
回答済み: Stephen23 2021 年 7 月 15 日
Lets just say that I have a matrix v as following:
v =
0 0 0 0 0 0
1 1 0 1 1 0
1 0 1 0 1 0
0 0 1 0 1 0
0 0 0 1 1 1
I want to get rows where alternating ones and zeros are occuring which are 3rd and 4th rows. By alternating ones and zeros, I mean a sequence of [1 0 1 0] at least.
  2 件のコメント
Walter Roberson
Walter Roberson 2021 年 7 月 14 日
How about if a row ends with 0 1 0 1, is that also alternating 0 and 1, or does the sequence have to start with a 1?
Awais Saeed
Awais Saeed 2021 年 7 月 14 日
Yes 0 1 0 1 would also be an alternating sequence.

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

採用された回答

Devanuj Deka
Devanuj Deka 2021 年 7 月 14 日
編集済み: Devanuj Deka 2021 年 7 月 14 日
% Get the dimensions of 'v'
sz = size(v); % Gives two numbers. sz(1) is the number of rows; sz(2) is the number of columns
% Initialize a zero vector, having as many elements as the number of rows
% in v. After the code is run, the i'th element of this vector will be 1 if
% the i'th row of 'v' has an alternating pattern
rows = zeros(1,sz(1));
% Look for the pattern
for i=1:sz(1) % Iterating through rows
count = 0; % Count increments if the next element in the row is different
for j=1:sz(2)-1 % Iterating through the elements of each row
if v(i,j)~=v(i,j+1) % If current element and next element are unequal, increment count
count = count+1;
else % If next element is equal to the current element, count is reset
count = 0;
end
if count==3 % Minimum 3 changes back to back are required for your pattern (0->1->0->1, or 1->0->1->0)
rows(j) = 1; % If pattern detected in i'th row, row(i) is set to 1
break; % Since pattern detected, no need to look for more in the same row.
end
end
end
rows_with_pattern = find(rows); % A vector containing the rows which have the alternating pattern
find(X) gives the indices of non-zero elements in an array X, which is why we initialized rows as a vector of zeroes.
  2 件のコメント
Awais Saeed
Awais Saeed 2021 年 7 月 15 日
Thanks. That did the trick.
Devanuj Deka
Devanuj Deka 2021 年 7 月 15 日
You're welcome!

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

その他の回答 (2 件)

Matt J
Matt J 2021 年 7 月 14 日
編集済み: Matt J 2021 年 7 月 14 日
v =[
0 0 0 0 0 0
1 1 0 1 1 0
1 0 1 0 1 0
0 0 1 0 1 0
0 0 0 1 1 1];
A=v(:,1:end-3); B=v(:,2:end-2); C=v(:,3:end-1); D=v(:,4:end);
pattern1=A==0 & B==1 & C==0 & D==1;
pattern2=A==1 & B==0 & C==1 & D==0;
rows=find( any(pattern1|pattern2,2) )
rows = 2×1
3 4
  1 件のコメント
Stephen23
Stephen23 2021 年 7 月 15 日
+1 nice use of MATLAB.

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


Stephen23
Stephen23 2021 年 7 月 15 日
v =[ 0 0 0 0 0 0; ...
1 1 0 1 1 0; ...
1 0 1 0 1 0; ...
0 0 1 0 1 0; ...
0 0 0 1 0 1; ... Walter Roberson's suggestion,
1 0 1 0 0 0; ... and reversed also.
0 1 0 1 0 1; ... Entire row alternating.
0 0 0 1 1 1];
X = any(diff(v,3,2)>3 | diff(~v,3,2)>3, 2)
X = 8×1 logical array
0 0 1 1 1 1 1 0

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by