the first time there is zero before and after a particular index
4 次查看(过去 30 天)
显示 更早的评论
Hi All, I have a matrix which looks like this:
1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 Let's say that I want to find out at what indices there is the first zero happening before and after index 20. How do we do this? So, in my example here, my points of interest will be: 7 (for before) and 37 (for after).
Thanks
0 个评论
采纳的回答
the cyclist
2017-7-30
Here is one straightforward way:
x = [1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0];
anchor = 20;
positionIndex = 1:numel(x);
lastPriorZero = max(find(x==0 & positionIndex<anchor))
firstLaterZero = min(find(x==0 & positionIndex>anchor))
0 个评论
更多回答(1 个)
Image Analyst
2017-7-30
If you want to find the 0's just outside the 1's, and you have the Image Processing Toolbox, you can do this:
x = [1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0]
dilatedx = imdilate(x, [1,1,1]) % Grow 1 regions outward
indexes = find(dilatedx ~= x) % Find locations of mismatches.
and you get
indexes =
4 7 37
as you should.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!