Question: MATLAB How many consecutive ones? Write a function ConsecutiveOnes to detect in a logical row array of any length the locations where a sequence of
MATLAB
How many consecutive ones?
Write a function ConsecutiveOnes to detect in a logical row array of any length the locations where a sequence of ones (trues) start, the length of the sequence, the positions where the sequence starts and stops, and the length of the sequences. If there is a single one, the location of the one should be indicated with a length of one. For example, for the input array [0 0 1 1 0 0 1] the function ConsecutiveOnes produces the output [0 0 2 0 0 0 1] indicating consecutive 1s are starting in position 3 and 7. There are 2 ones in the run that starts in position 2 and there is 1 ones in the run starting in position 7. The sequences start at [3 7] and stop at [4 7]. The length of the runs are [2 1].
Your solution can use any programming approach. Additional examples:




inputArray = [0,0,1,1,0,0,0,1,1,1); >indicator, startPositionofAllOnes,endPositionOfAllones, lengthOfOnes] indicator- FindConsecutive0nes(inputArray) startPositionOfALLOnes endPositionOfALLOnes length0fones = 4 10 inputArray= [0, 1, 1, 0, 1]; [indicator, startPositionOfALLOnes,endPositionOfALLOnes,lengthofAllones] indicator tonorat tones, = FindConsecutiveOnes (inputArray) startPositionOfALiones endPositionOfALLOnes lengthofAllOnes =
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
