Longest Run Tosses Matlab

Write an efficient function which computes the length of the longest run of heads in an arbitrary sequence of heads and tails ?
The excercise is about a coin toss and i would really appreciate if someone could help me with this excercise.

6 Comments

What have you done so far? What specific problems are you having with your code?
BK, the purpose of answering questions on this forum is not just to solve a problem for the OP. It is about creating a knowledge-base for MATLAB related issues. Therefore, even if you found a different solution, the answers to this question are still useful for anyone else who stumbles upon a similar problem and come to this page. Therefore, I am pasting your original question as a comment. Otherwise, the answers lose their value.
Ameer Hamza
Ameer Hamza on 5 Jun 2020
Edited: Ameer Hamza on 6 Jun 2020
Berkay Kül Original Question
Title: Longest Run of heads in a sequence of coin tosses
Write an efficient function which computes the length of the longest run of heads in an arbitrary sequence of heads and tails ?
The excercise is about a coin toss and i would really appreciate if someone could help me with this excercise.
@KBB's flag: I want to delete my question, because I found a code on my own which is working
KBB, in general, you should search for an answer before asking a question. Once you ask a question, volunteers may invest their time into answering it and at that point, you can't give them their time back. The return on thier investment is an answer that may benefit others in the future.
KBB it's really clear that your professor found this link ;) and thank you for disrespecting others effort.
(Answers Dev) Restored edit

Sign in to comment.

Answers (3)

Walter Roberson
Walter Roberson on 5 Jun 2020
at any one time you only need a few pieces of information:
  • are you currently in a run
  • length of the current run
  • maximum length of run encountered so far
if you are not in a run and you encounter a T then move on to the next entry
if you are in a run and you encounter a T then compare the length of the current run to the maximum so far to determine if you have a new record. Afterwards either way mark yourself as no longer being in a run. then move on to the next entry.
if you are in a run and encounter a H then increment the length of the current run. Then move on to the next entry.
I left out a detail that you should be able to catch.
You can get away with using just two variables aside from the loop index.
KSSV
KSSV on 5 Jun 2020
Edited: KSSV on 5 Jun 2020
N = 2 ; % [1- heads, 2-tails]
toss = 100 ; %number of tossings
state = zeros(toss,1) ;
for i = 1:toss
state(i) = randperm(N,1) ;
end
% split the states which are continuous 1 and/or 2
S = mat2cell(state, diff([0; find(diff(state)); size(state,1)]));
% Get the length of sequence of states
L = cellfun(@length,S) ;
% Get the maximum length sequence
[val,id] = max(L) ;
fprintf("State:%d is repeated maximum of %d times\n",unique(S{id}),val)

2 Comments

this was clearly a homework assignment :(
You should understand first how/ what the code is.
Thanks is accepting the answer. :)

Sign in to comment.

Stephen23
Stephen23 on 5 Jun 2020
Edited: Stephen23 on 5 Jun 2020
Simpler and more efficient:
>> N = 17; % number of tosses
>> V = randi(2,1,N) % 1=tails 2=heads
V =
2 1 2 2 2 2 2 2 1 1 2 2 1 1 1 2 2
>> D = diff([false,V==2,false]); % 2=heads
>> L = find(D<0)-find(D>0);
>> max(L)
ans = 6

Categories

Find more on Mathematics in Help Center and File Exchange

Tags

No tags entered yet.

Asked:

KB
on 5 Jun 2020

Commented:

on 12 Oct 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!