Change sequence of consecutive trues to falses, in logical array
    6 views (last 30 days)
  
       Show older comments
    
Hello guys!
I would like to find a fast procedure to change from true to false the consecutive trues in a logical array excluding only the first and the last true in the sequence.
For instance:
x=[true;false;false;true;true;true;true;true];
Desired output array should be:
output=[true;false;false;true;false;false;false;true];
Hope the question is clear.
Thank you!
0 Comments
Accepted Answer
  Bruno Luong
      
      
 on 13 Oct 2022
        
      Edited: Bruno Luong
      
      
 on 13 Oct 2022
  
      x=[true;false;false;true;true;true;true;true;false;true]'
x & ~([false,x(1:end-1)]&[x(2:end),false])
0 Comments
More Answers (1)
  Chunru
      
      
 on 13 Oct 2022
        x=[true;false;false;true;true;true;true;true]'
output = x;
dx = diff(x(1:end-1))
output1 = output(2:end-1);
output1(output1 & (dx==0)) = false;
% Desired output array should be:
output(2:end-1) = output1;
output
% Desired
[true;false;false;true;false;false;false;true]'
See Also
Categories
				Find more on Matrix Indexing in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

