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

3 次查看(过去 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 中查找有关 Data Types 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by