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

9 visualizaciones (últimos 30 días)
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!

Respuesta aceptada

David Hill
David Hill el 28 de Sept. de 2022
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

Más respuestas (0)

Categorías

Más información sobre Performance and Memory en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by