How can I find elements in sequence in an array?
13 次查看(过去 30 天)
显示 更早的评论
I have an array of numbers. I want to find set of numbers for which difference of two consecutive numbers is 1. For example, I have a sequence [2,3,6,8,9,10,12,14,16,17]. How can I extract position of (2,3), (8,9,10), (16,17)? I also want to save the output in a new array.
0 个评论
采纳的回答
Azzi Abdelmalek
2017-5-23
v=[2,3,6,8,9,10,12,14,16,17]
id=[10 diff(v) 10]==1
ii1=strfind(id,[0 1])
ii2=strfind(id,[ 1 0])
3 个评论
Jan
2017-5-23
To avoid a collision with the elements on the margins:
id = [false, diff(v)==1, false];
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!