How to return logical array with only the longest row-consecutive equal to true

조회 수: 7 (최근 30일)
How to take logical array of rows and columns and return the same size logical array but all values beside the location of the longest row-consecutive true values are false. If multiple rows have the same larget consecutive true values, then the first row with the largest consecutive true values is returned. I have working code but I'd like to vectorize it as it currently is loop-based and takes longer than I'd like. Oh and most logical array inputs have between 1 and 50 rows, and the number of columns are greater than 200,000.
i.e.
input = [0 0 0 1 1 1 1 0 0 1 1 0;
1 1 1 0 0 1 0 0 1 1 1 1;
0 1 0 1 0 1 1 1 1 1 1 1];
output = [0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1 1 1];
input = [0 0 1 1 1 1 1 1 0 0 0 0;
0 0 1 0 0 1 0 0 1 0 0 1;
1 1 1 1 1 1 0 0 0 0 0 0];
output = [0 0 1 1 1 1 1 1 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0];
Thank you so much!

채택된 답변

David Hill
David Hill 2022년 9월 28일
I = [0 0 1 1 1 1 1 1 0 0 0 0;
0 0 1 0 0 1 0 0 1 0 0 1;
1 1 1 1 1 1 0 0 0 0 0 0];
i=[zeros(size(I,1),1),I,zeros(size(I,1),1)];
d=diff(i,1,2);
[r,c]=find(d==1);
[R,C]=find(d==-1);
[r,idx]=sort(r);c=c(idx);
[R,idx]=sort(R);C=C(idx);
[~,idx]=max(C-c);
O=zeros(size(I));
O(r(idx),c(idx)+1:C(idx))=1
O = 3×12
0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by