如何在Fortran中找到统计模式

发布于 2024-12-26 04:25:23 字数 1409 浏览 2 评论 0原文

我正在尝试编写一个程序来查找整数数组的平均值、中位数和众数,但在查找众数时遇到一些复杂情况。以下是我迄今为止编写的代码。

首先,程序将提示用户输入要输入的整数数量的值,然后请求输入该整数数量。然后将整数按升序排序,并找到平均值和中位数。

我遇到的问题是当我尝试获取模式时。我能够计算重复值出现的次数。通过找到出现次数最多的值,我们就能够找到众数。但我不确定如何做到这一点。 Fortran 中是否有任何内在函数可以计算输入值的出现次数以及出现次数最多的值

  PROGRAM STATISTICS
  !Created by : Rethnaraj Rambabu
  IMPLICIT NONE

  REAL, DIMENSION(:), ALLOCATABLE:: VAL
  REAL TEMP, MEDIAN
  REAL EVEN, MEAN, SUM, FMODE

  INTEGER N, I,J

  WRITE(*,*)' WHAT IS THE VALUE FOR N? '
  READ(*,*) N
  ALLOCATE(VAL(N))

  WRITE(*,*) 'ENTER THE NUMBERS'
  OPEN(1,FILE='FILE.TXT')
  READ(1,*)(VAL(I),I=1,N)
  CLOSE(1)
  WRITE(*,*) VAL

  !/---FOR SORTING----/!

  DO I=1,N-1
    DO J=1,N-1
      IF(VAL(J) > VAL(J+1)) THEN
      TEMP=VAL(J)
      VAL(J)=VAL(J+1)
      VAL(J+1)=TEMP
      END IF
    END DO
  END DO

  WRITE(*,*) VAL

  !/-----MEDIAN----/!

  IF ((N/2*2) /= N) THEN
      MEDIAN=VAL((N+1)/2)
    ELSE IF ((N/2*2) == N) THEN
      EVEN= (VAL(N/2)+VAL((N+2)/2))
      MEDIAN=EVEN/2
  END IF

  WRITE(*,*)'MEDIAN=', MEDIAN

  !/----MEAN----/
  SUM=0
  DO I=1,N
    SUM=SUM+VAL(I)
  END DO
    MEAN=SUM/N

  WRITE(*,*)'MEAN=', MEAN

  !/------MODE----/
  FMODE=1
  DO I=1,N-1
    IF (VAL(I) == VAL(I+1)) THEN
    FMODE=FMODE+1
  END IF
  END DO

  WRITE(*,*)FMODE

  END PROGRAM

FILE.TXT 包含

10 8 1 9 8 9 9 7 5 9 3 5 6

I'm trying to write a program to find the mean, median, mode of an integer array but am having some complications in finding the mode. The following is the code that I've written so far.

First, the program will prompt user to enter a value for the number of integers that will be entered followed by request to enter that number of integers. The integers are then sorted in ascending order and the mean and median are found.

The problem I am having is when I try to get the mode. I am able to count the number of occurrence of a repetitive value. By finding the value with highest occurrence, we'll be able to find Mode. But I am unsure how to do this. Is there any intrinsic function in Fortran to calculate number of occurrence of input values and the value with highest occurrence?

  PROGRAM STATISTICS
  !Created by : Rethnaraj Rambabu
  IMPLICIT NONE

  REAL, DIMENSION(:), ALLOCATABLE:: VAL
  REAL TEMP, MEDIAN
  REAL EVEN, MEAN, SUM, FMODE

  INTEGER N, I,J

  WRITE(*,*)' WHAT IS THE VALUE FOR N? '
  READ(*,*) N
  ALLOCATE(VAL(N))

  WRITE(*,*) 'ENTER THE NUMBERS'
  OPEN(1,FILE='FILE.TXT')
  READ(1,*)(VAL(I),I=1,N)
  CLOSE(1)
  WRITE(*,*) VAL

  !/---FOR SORTING----/!

  DO I=1,N-1
    DO J=1,N-1
      IF(VAL(J) > VAL(J+1)) THEN
      TEMP=VAL(J)
      VAL(J)=VAL(J+1)
      VAL(J+1)=TEMP
      END IF
    END DO
  END DO

  WRITE(*,*) VAL

  !/-----MEDIAN----/!

  IF ((N/2*2) /= N) THEN
      MEDIAN=VAL((N+1)/2)
    ELSE IF ((N/2*2) == N) THEN
      EVEN= (VAL(N/2)+VAL((N+2)/2))
      MEDIAN=EVEN/2
  END IF

  WRITE(*,*)'MEDIAN=', MEDIAN

  !/----MEAN----/
  SUM=0
  DO I=1,N
    SUM=SUM+VAL(I)
  END DO
    MEAN=SUM/N

  WRITE(*,*)'MEAN=', MEAN

  !/------MODE----/
  FMODE=1
  DO I=1,N-1
    IF (VAL(I) == VAL(I+1)) THEN
    FMODE=FMODE+1
  END IF
  END DO

  WRITE(*,*)FMODE

  END PROGRAM

The FILE.TXT contains

10 8 1 9 8 9 9 7 5 9 3 5 6

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慵挽 2025-01-02 04:25:23

但是,该怎么做呢?或者Fortran中是否有任何内部函数可以计算输入值出现的次数以及出现次数最多的值。

不,没有。您必须手动计算众数。

以下代码应该可以工作(在排序数组上):

FMODE = VAL(1)
COUNT = 1
CURRENTCOUNT = 1
DO I = 2, N
    ! We are going through the loop looking for values == VAL(I-1)...
    IF (VAL(I) == VAL(I-1)) THEN
        ! We spotted another VAL(I-1), so increment the count.
        CURRENTCOUNT = CURRENTCOUNT + 1
    ELSE
        ! There are no more VAL(I-1)
        IF (CURRENTCOUNT > COUNT) THEN
            ! There were more elements of value VAL(I-1) than of value FMODE
            COUNT = CURRENTCOUNT
            FMODE = VAL(I-1)
        END IF
        ! Next we are looking for values == VAL(I), so far we have spotted one...
        CURRENTCOUNT = 1
    END
END DO
IF (CURRENTCOUNT > COUNT) THEN
    ! This means there are more elements of value VAL(N) than of value FMODE.
    FMODE = VAL(N)
END IF

说明:

我们将迄今为止最好的模式保存在 FMODE 变量中,并将 FMODE 的计数保存在COUNT 变量。当我们单步遍历数组时,我们会在 CURRENTCOUNT 变量中计算出等于我们现在看到的命中数。

如果我们查看的下一项与上一项相同,我们只需增加 CURRENTCOUNT 即可。如果不同,那么我们需要重置 CURRENTCOUNT,因为我们现在将计算下一个元素的重复次数。

在重置 CURRENTCOUNT 之前,我们会检查它是否大于之前的最佳结果,如果是,我们会覆盖之前的最佳结果(FMODECOUNT< /code> 变量)并获得新的最佳结果(无论 VAL(I)CURRENTCOUNT 处的结果),然后再继续。

这种重置不会在循环结束时发生,因此我在末尾插入了另一个检查,以防最频繁的元素恰好是循环的最后一个元素。在这种情况下,我们会覆盖 FMODE,就像我们在循环中所做的那样。

But, how to do that? Or is there any intrinsic function in Fortran to calculate number of occurrence of input values and the value with highest occurrence.

No, there is not. You'll have to calculate the mode by hand.

The following code should work (on a sorted array):

FMODE = VAL(1)
COUNT = 1
CURRENTCOUNT = 1
DO I = 2, N
    ! We are going through the loop looking for values == VAL(I-1)...
    IF (VAL(I) == VAL(I-1)) THEN
        ! We spotted another VAL(I-1), so increment the count.
        CURRENTCOUNT = CURRENTCOUNT + 1
    ELSE
        ! There are no more VAL(I-1)
        IF (CURRENTCOUNT > COUNT) THEN
            ! There were more elements of value VAL(I-1) than of value FMODE
            COUNT = CURRENTCOUNT
            FMODE = VAL(I-1)
        END IF
        ! Next we are looking for values == VAL(I), so far we have spotted one...
        CURRENTCOUNT = 1
    END
END DO
IF (CURRENTCOUNT > COUNT) THEN
    ! This means there are more elements of value VAL(N) than of value FMODE.
    FMODE = VAL(N)
END IF

Explanation:

We keep the best-so-far mode in the FMODE variable, and the count of the FMODE in the COUNT variable. As we step through the array we count the number of hits that are equal to what we are looking at now, in the CURRENTCOUNT variable.

If the next item we look at is equal to the previous, we simply increment the CURRENTCOUNT. If it's different, then we need to reset the CURRENTCOUNT, because we will now count the number of duplications of the next element.

Before we reset the CURRENTCOUNT we check if it's bigger than the previous best result, and if it is, we overwrite the previous best result (the FMODE and COUNT variables) with the new best results (whatever is at VAL(I) and CURRENTCOUNT), before we continue.

This reset doesn't happen at the end of the loop, so I inserted another check at the end in case the most frequent element happens to be the final element of the loop. In that case we overwrite FMODE, like we would have done in the loop.

纵性 2025-01-02 04:25:23

它有点长,您可能可以删除可选参数,但这里提供了一个示例 here 。他们使用此处实现的快速排序算法。

或者,您可以使用

integer function mode(arr) result(m)
  implicit none

  integer, dimension(:), intent(in) :: arr

  ! Local variables
  integer, dimension(:), allocatable :: counts
  integer :: i, astat
  character(len=128) :: error_str

  ! Initialise array to count occurrences of each value.
  allocate(counts(minval(arr):maxval(arr)), stat=astat, errmsg=error_str)
    if (astat/=0) then
      print'("Allocation of counts array failed.")'
      print*, error_str
    end if

  counts = 0

  ! Loop over inputted array, counting occurrence of each value.
  do i=1,size(arr)
      counts(arr(i)) = counts(arr(i)) + 1
  end do

  ! Finally, find the mode
  m = minloc(abs(counts - maxval(counts)),1)

end function mode

这不需要任何排序。

It is a bit lengthy, you could probably get rid of the optional argument, but there is an example provided here. They use the quick sort algorithm as implemented here.

Alternatively, you could use

integer function mode(arr) result(m)
  implicit none

  integer, dimension(:), intent(in) :: arr

  ! Local variables
  integer, dimension(:), allocatable :: counts
  integer :: i, astat
  character(len=128) :: error_str

  ! Initialise array to count occurrences of each value.
  allocate(counts(minval(arr):maxval(arr)), stat=astat, errmsg=error_str)
    if (astat/=0) then
      print'("Allocation of counts array failed.")'
      print*, error_str
    end if

  counts = 0

  ! Loop over inputted array, counting occurrence of each value.
  do i=1,size(arr)
      counts(arr(i)) = counts(arr(i)) + 1
  end do

  ! Finally, find the mode
  m = minloc(abs(counts - maxval(counts)),1)

end function mode

This doesn't require any sorting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文