Re: elegant solution for counting runs



On 30 dec, 09:34, Arjen Markus <arjen.mar...@xxxxxxxxxx> wrote:
On 30 dec, 02:24, analys...@xxxxxxxxxxx wrote:

This is not necessarily a fortran question but

I have a sequence of heads and tails and I want to count the number of
runs (of either kind) of length 1, 2,3,,,etc.

I spent a few hours on it and came up with a horrific solution that
has to test (within a loop through the sequence) for the first
observation , last observation and whether the last observation is a
run of length 1.

Surely theres a more elegant way and I would appreciate any hlep with
it.

I do not have a ready solution, but have you considered using the
cshift
function? I'd say, something like:

- Shift the array by 1 and check if the entries in the new array and
the
original match. If not, you have found a run of length 1. Keep the
entries that did match.
- Shift the array by 2 and check again, with the array of previous
matches.
The entries that don't match indicate runs of length 2 and so on.

Just a sketch, mind you. Hm, a nice puzzle ...

Regards,

Arjen

No, I retract that. Here is a small program that will count the
runs - I think it is fairly elegant, even though it loops over
the data explicitly:

! runs.f90 --
! COunt the runs of length 1, 2, ...
!
program count_runs
implicit none

integer, dimension(10) :: data = (/ 1, 0, 1, 1, 0, 0, 0, 1, 1,
0 /)
integer, dimension(10) :: runs
integer :: i
integer :: prev
integer :: count

runs = 0

count = 1
prev = data(1)
do i = 2,size(data)
if ( data(i) .ne. prev ) then
runs(count) = runs(count) + 1
prev = data(i)
count = 1
else
count = count + 1
endif
enddo
runs(count) = runs(count) + 1

write(*,*) runs
endprogram

(A solution with array operations like I sketched before should be
possible too, but this is very straightforward!)

Regards,

Arjen
.



Relevant Pages

  • Re: Random Function
    ... After a given time you exit the loop. ... An even better way would be to read your Table into an array and "shuffle" ... Afterwards you read e.g. the first 5 entries. ...
    (microsoft.public.access.modulesdaovba)
  • Re: The name of script itself
    ... You need this instead if you want to do it in a loop: ... even though he's accessing the array in random order, ... Prev by Date: ...
    (comp.lang.awk)
  • Re: looking for random array reshuffling algorithm
    ... >reshuffle the content of a number array (16+ entries) over and over ... And doesn't loop for a long while. ... This may be what you want: Let m be the size of the array. ... lexicographically ordered table of all permutations. ...
    (comp.programming)
  • looking for random array reshuffling algorithm
    ... I'm looking for a fast and reversible algorithm which can randomly ... reshuffle the content of a number array (16+ entries) over and over ... And doesn't loop for a long while. ...
    (comp.programming)
  • Muti Dimmed Array Problem
    ... I am trying to create an array that will eventually loop to update ... corresponding record in a database. ... Prev by Date: ...
    (microsoft.public.scripting.vbscript)