How to find out zeros and their indices in a matrix?
1 view (last 30 days)
Show older comments
I have an input matrix of size 3*13 double:
input=[0 0 1 0 0 0 1 1 1 0 0 0 0;
0 1 0 0 1 1 0 0 0 1 0 1 0;
0 1 1 1 1 0 0 0 1 0 0 1 0];
I want to know how many zeros subsequently have in
input(2,:)
The output should be:
output=[1 2 3 1 1];
I also want to know their starting index of zeros. It should look like:
index=[1 3 7 11 13];
How can i solve this problem?
0 Comments
Accepted Answer
Bruno Luong
on 21 Jul 2020
Edited: Bruno Luong
on 21 Jul 2020
v=input(2,:)>0; % remove >0 if your matrix contains only 0 or 1
d = diff([1, v, 1]);
index = find(d==-1)
output = find(d==1)-index
See Also
Categories
Find more on Logical 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!