Info
此问题已关闭。 请重新打开它进行编辑或回答。
How do i find the index from a logical array that meets the condition, without using for loop?
1 次查看(过去 30 天)
显示 更早的评论
I would like to find the minimum index i from a logical array a such that n consecutive elements starting from i are all 1. For example: a=[1 0 1 1 1 0 1 1 1 1], if n=1 return i=1; if n=2 or 3,then return i=3; if n=4 then return i=7. I don't want to use for loop.
0 个评论
回答(1 个)
Guillaume
2016-5-10
This is a classic run length encoding problem, there are several functions that calculate that for you on the FileExchange and plenty of answers on this forum. Alternatively, a combination of diff and find is all you need:
a = [1 0 1 1 1 0 1 1 1 1]
%find location and lengths of runs
transitions = diff([0 a 0]);
runstarts = find(transitions == 1);
runpastends = find(transitions == -1);
runlengths = runpastends - runstarts;
%display
sortrows(table(runlengths', runstarts', 'VariableNames', {'length', 'start'}))
0 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!