-
1 Comment
function val=longrun(a)
a_len=length(a); % Calculate the length of a
c=ones(a_len,1); % Initialize the consecutive times of the corresponding position
for num_a=1:(a_len-1)
n_val=1;
for next_a=(num_a+1):a_len
if a(num_a)==a(next_a) % If this number is equal to the following number, then n_val+1, and the next cycle
n_val=n_val+1;
c(num_a)=n_val;
else
c(num_a)=n_val; % If this number is not equal to the following number, assign the current n_val to the corresponding number of consecutive times, and jump out of the inner for loop
break
end
end
end
a_conti=max(c); %Find the largest number of consecutive times in the c array
max_location=find(c==a_conti); % Corresponds to the position of the maximum consecutive times
val=a(max_location); % Find out the corresponding position in a
end
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!