To find the longest sequence of the same values in an array in Matlab, you can use the diff
function to compute the difference between consecutive elements of the array. Then you can use the find
function to determine where the difference changes sign (from positive to negative or vice versa) and look at the differences between these indices to determine the lengths of the runs of consecutive elements with the same value. Finally, you can report the value of the longest run using indexing.
Here's an example implementation:
main.m467 chars18 lines
This will output:
main.m31 chars2 lines
indicating that the longest run of the same value in the array is 5, and it occurs five times in a row.
gistlibby LogSnag