Change sequence of consecutive trues to falses, in logical array
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Bruno Luong
2022-10-13
编辑:Bruno Luong
2022-10-13
x=[true;false;false;true;true;true;true;true;false;true]'
x & ~([false,x(1:end-1)]&[x(2:end),false])
0 个评论
更多回答(1 个)
Chunru
2022-10-13
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]'
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Multidimensional Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!