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

4 ビュー (過去 30 日間)
Ryan
Ryan 2022 年 9 月 28 日
コメント済み: Ryan 2022 年 9 月 28 日
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 Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by