Counting Specific Number of Consecutive Values in a Matrix
Show older comments
I have a matrix composed of ones and zeros.
I need to idenity where there are three 1s in each COLUMN
1 0 0 1 1
1 0 1 1 1
1 1 1 0 1
1 0 1 1 1
1 1 0 1 1
0 1 1 1 1
1 1 1 0 1
I want to have the code identify if
- there are three consecutive 1s in the first through third column, and
- that there are two sets of consecutive 1s in the fourth column.
I then want to compare this matrix with another matrix, and find where they are similar in the exact locations of the three consecutive 1s.
Accepted Answer
More Answers (1)
A = [1 0 0 1 1
1 0 1 1 1
1 1 1 0 1
1 0 1 1 1
1 1 0 1 1
0 1 1 1 1
1 1 1 0 1];
% First column
x = A(:,1) ;
f = find(diff([0 ;x ;0]==1));
id = f(1:2:end-1); % Start indices
N = f(2:2:end)-p; % Consecutive ones’ counts
Also have a look on ismember, this would be useful to check two different matrices have common eements.
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!