gpt4 book ai didi

linux - 按间隔对数字进行分组 : I have an array of numbers between 0. 0-3.6

转载 作者:太空宇宙 更新时间:2023-11-04 05:30:31 28 4
gpt4 key购买 nike

我有一个 0.0-3.6 之间的数字数组,我想知道有多少个数字在这个区间内:0.0-0.2、0.2-0.4 等等,有人可以帮助我吗?多谢...我在 Fortran 中尝试过:

IMPLICIT NONE

INTEGER :: acoun, bcoun, ccoun, dcoun, ecoun, fcoun, gcoun, hcoun, icoun
INTEGER :: jcoun, kcoun, lcoun, mcoun, ncoun, ocoun, pcoun, qcoun, ia
INTEGER :: i1(4), i2(30), i3(30), i4(30), i5(30), i6(30), i7(30), i8(30), i9(30), i10(30), &
i11(30), i12(30), i13(30), i14(30), i15(30), i16(30), i17(30)
REAL :: a

OPEN(UNIT=10,FILE='DAN1',STATUS='OLD')

acoun=0.0
bcoun=0.0
ccoun=0.0
dcoun=0.0
ecoun=0.0
fcoun=0.0
gcoun=0.0
hcoun=0.0
icoun=0.0
jcoun=0.0
kcoun=0.0
lcoun=0.0
mcoun=0.0
ncoun=0.0
ocoun=0.0
pcoun=0.0
do i=1,119
READ(10,*)a
if (( a < 0.2d0 ).and.(a > 0.0d0) )then
i1(acoun)=i
acoun=acoun+1
elseif (( a < 0.4d0 ).and.(a > 0.2d0) )then
i2(bcoun)=i
bcoun=bcoun+1
elseif (( a < 0.6d0 ).and.(a > 0.4d0) )then
i3(ccoun)=i
ccoun=ccoun+1
elseif (( a < 0.8d0 ).and.(a > 0.6d0) )then
i4(dcoun)=i
dcoun=dcoun+1
elseif (( a < 1.0d0 ).and.(a > 0.8d0) )then
i5(ecoun)=i
ecoun=ecoun+1
elseif (( a < 1.2d0 ).and.(a > 1.0d0) )then
i6(fcoun)=i
fcoun=fcoun+1
elseif (( a < 1.4d0 ).and.(a > 1.2d0) )then
i7(gcoun)=i
gcoun=gcoun+1
elseif (( a < 1.6d0 ).and.(a > 1.4d0) )then
i8(hcoun)=i
hcoun=hcoun+1
elseif (( a < 1.8d0 ).and.(a > 1.6d0) )then
i9(icoun)=i
icoun=icoun+1 elseif (( a < 2.0d0 ).and.(a > 1.8d0) )then
i10(jcoun)=i
jcoun=jcoun+1
elseif (( a < 2.2d0 ).and.(a > 2.0d0) )then
i11(kcoun)=i
kcoun=kcoun+1
elseif (( a < 2.4d0 ).and.(a > 2.2d0) )then
i12(lcoun)=i
lcoun=lcoun+1
elseif (( a < 2.6d0 ).and.(a > 2.4d0) )then
i13(mcoun)=i
mcoun=mcoun+1
elseif (( a < 2.8d0 ).and.(a > 2.6d0) )then
i14(ncoun)=i
ncoun=ncoun+1
elseif (( a < 3.0d0 ).and.(a > 2.8d0) )then
i15(ocoun)=i
ocoun=ocoun+1
elseif (( a < 3.2d0 ).and.(a > 3.0d0) )then
i16(pcoun)=i
pcoun=pcoun+1
elseif (a > 3.2d0)then
i17(qcoun)=i
qcoun=qcoun+1
endif
enddo
WRITE(*,*) i1(acoun),acoun
WRITE(*,*) i2(bcoun),bcoun
WRITE(*,*) i3(ccoun),ccoun
WRITE(*,*) i4(dcoun),dcoun
WRITE(*,*) i5(ecoun),ecoun
WRITE(*,*) i6(fcoun),fcoun
WRITE(*,*) i7(gcoun),gcoun
WRITE(*,*) i8(hcoun),hcoun
WRITE(*,*) i9(icoun),icoun
WRITE(*,*) i10(jcoun),jcoun
WRITE(*,*) i11(kcoun),kcoun
WRITE(*,*) i12(lcoun),lcoun
WRITE(*,*) i13(mcoun),mcoun
WRITE(*,*) i14(ncoun),ncoun
WRITE(*,*) i15(ocoun),ocoun
WRITE(*,*) i16(pcoun),pcoun
WRITE(*,*) i17(qcoun),qcoun

close(10)

ENDPROGRAM sorting"

此文件给出了一些值,但它们不正确,该程序在每个间隔中都进行了计数。

我在 bash 中尝试过这个

我尝试在 bash 中使用 if 语句,例如:

if [$var > 0.0 -a 0.2 < $var]then 
echo "$var" >> file
wc -l file

但它也不起作用

非常感谢您的帮助......

最佳答案

Bash 不支持浮点运算,但 awk 支持:

假设文件包含所有数字,在单独的行中,此脚本应该执行您想要的操作:

$ awk '{a[int($1/0.2)]++}END{for(i in a)print i*0.2 "-" (i+1)*0.2, a[i]}' file | sort -n

关于linux - 按间隔对数字进行分组 : I have an array of numbers between 0. 0-3.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20886904/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com