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!
0 个评论
采纳的回答
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
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!