Matrix 如何在fortran中计算矩阵中数字的出现次数?

Matrix 如何在fortran中计算矩阵中数字的出现次数?,matrix,count,fortran,Matrix,Count,Fortran,我使用的是fortran 90,我希望当两个数字出现在一个数组中时,计算出现的次数 flag=0 q=0 do k=1,ncolumns if (conn(m,k)==i .and. conn(m,k)==l) flag=1 enddo if (flag==1) q=q+1 write (*,*) q 这里,connm,k是矩阵,由m行和k列组成。我想读取connm,k,并在connm,k中包含数字I和l时计算出现的次数。我知道上面

我使用的是fortran 90,我希望当两个数字出现在一个数组中时,计算出现的次数

    flag=0
    q=0
    do k=1,ncolumns
      if (conn(m,k)==i .and. conn(m,k)==l) flag=1
    enddo
    if (flag==1) q=q+1 
    write (*,*) q
这里,connm,k是矩阵,由m行和k列组成。我想读取connm,k,并在connm,k中包含数字I和l时计算出现的次数。我知道上面的代码不起作用,因为它只输出0,因为if循环有问题。但我不能用“.”或“.”因为我想知道当我和我都包括在康纳姆,k中时的计数。我如何检查conn中是否包含编号I和l

我修改了上面的代码

    ncolumns=2
    flag=0
    q=0
    do k=1,ncolumns
      !!!if (conn(m,k)==i .and. conn(m,k)==l) flag=1
      if (((conn(m,1)==i).and.(conn(m,2)==l)).or.((conn(m,1)==l).and.(conn(m,2)==i))) flag=1
    enddo
    if (flag==1) q=q+1 
    write (*,*) q
这很好,但正如您所看到的,这段代码很荒谬,因为我需要手动定义k,特别是当'ncolumns'是一个巨大的数字时。如何使用索引执行此操作


同样,我如何检查矩阵中是否包含2个或更多特定数字,如fortran中的connm,k?谢谢

像这样的东西应该能满足你的需要:

  nums = [2,12,-4,99]  ! an array of the numbers you're looking for
  q = 0                ! the count of rows containing all the numbers in nums

  DO ix = 1, SIZE(conn,1)       ! the number of rows in conn
     nmatches = 0               ! the number of elements of nums found in conn(ix,:)
     DO jx = 1, SIZE(nums)
        IF(ANY(conn(ix,:)==nums(jx))) nmatches = nmatches+1    ! figure this out yourself
     END DO
     ! if there are as many matches in this row as there are elements in nums, add 1 to q
     IF(nmatches==SIZE(nums)) q = q+1    
  END DO

像这样的东西应该能满足你的需要:

  nums = [2,12,-4,99]  ! an array of the numbers you're looking for
  q = 0                ! the count of rows containing all the numbers in nums

  DO ix = 1, SIZE(conn,1)       ! the number of rows in conn
     nmatches = 0               ! the number of elements of nums found in conn(ix,:)
     DO jx = 1, SIZE(nums)
        IF(ANY(conn(ix,:)==nums(jx))) nmatches = nmatches+1    ! figure this out yourself
     END DO
     ! if there are as many matches in this row as there are elements in nums, add 1 to q
     IF(nmatches==SIZE(nums)) q = q+1    
  END DO

根据注释,如果conn中有3行同时包含3和12等两个元素,则打印的q应为3。 如果你有Fortran95,你可以用一个循环来实现这一点。我忘了它是否在90规范或更高版本中。 以下是一个例子:

Program Test_Count

  Implicit None

  Real(Kind=8), Dimension(3)   :: nums = (/-2.0_8 , -3.0_8 , -4.0_8/)
  Real(Kind=8), Dimension(4,4) :: test
  Logical, Dimension(4) :: Mask
  Integer :: i,j,NumberOfLines

  ! Fill test
  Do i = 1,4
    Do j = 1,4
      test(i,j) = -4.0_8 + (j -1)
    End Do
  End Do

  ! Count the row that have all the nums in them
  Mask = any(test == nums(1),2)
  Do i = 2,3
    Mask = Mask .and. any(test == num2(i),2)
  End Do
  NumberOfLines = count(Mask)

  Write(*,*) NumberOfLines ! Prints out 4.

End Program Test_Count

根据注释,如果conn中有3行同时包含3和12等两个元素,则打印的q应为3。 如果你有Fortran95,你可以用一个循环来实现这一点。我忘了它是否在90规范或更高版本中。 以下是一个例子:

Program Test_Count

  Implicit None

  Real(Kind=8), Dimension(3)   :: nums = (/-2.0_8 , -3.0_8 , -4.0_8/)
  Real(Kind=8), Dimension(4,4) :: test
  Logical, Dimension(4) :: Mask
  Integer :: i,j,NumberOfLines

  ! Fill test
  Do i = 1,4
    Do j = 1,4
      test(i,j) = -4.0_8 + (j -1)
    End Do
  End Do

  ! Count the row that have all the nums in them
  Mask = any(test == nums(1),2)
  Do i = 2,3
    Mask = Mask .and. any(test == num2(i),2)
  End Do
  NumberOfLines = count(Mask)

  Write(*,*) NumberOfLines ! Prints out 4.

End Program Test_Count

您还可以使用虚拟矩阵虚拟矩阵填充值位于您要搜索的矩阵中的值,然后对虚拟矩阵求和以获得count num_条目:

    nums = [2,12,-4,99]
    do i=1,size(nums)     ! loop over the values you are looking for  
      dummy_mat = 0 ! zero out dummy matrix that is the same size as your value matrix           
      where (value_mat(:,:) == nums(i))    
        dummy_mat(:,:) = 1
      end where
      num_entries(i) = SUM(dummy_mat)
    end do

您还可以使用虚拟矩阵虚拟矩阵填充值位于您要搜索的矩阵中的值,然后对虚拟矩阵求和以获得count num_条目:

    nums = [2,12,-4,99]
    do i=1,size(nums)     ! loop over the values you are looking for  
      dummy_mat = 0 ! zero out dummy matrix that is the same size as your value matrix           
      where (value_mat(:,:) == nums(i))    
        dummy_mat(:,:) = 1
      end where
      num_entries(i) = SUM(dummy_mat)
    end do

@“高性能标记”第一个可能更接近“conn”是一个矩阵,我想知道有多少行“conn”同时有2个或更多的数字,比如3和12。并统计它的发生。例如,如果conn中有3行同时包含3和12等两个元素,则打印的q应为3。@HighPerformanceMark第二个示例代码的“ncolumns=2”就是一个示例。实际上,康涅狄格是一个巨大的矩阵。它是5000行x 15列的矩阵,由许多数据组成blocks@HighPerformanceMark第一个可能更近一些“conn”是一个矩阵,我想知道有多少行“conn”同时有2个或更多的数字,比如3和12。并统计它的发生。例如,如果conn中有3行同时包含3和12等两个元素,则打印的q应为3。@HighPerformanceMark第二个示例代码的“ncolumns=2”就是一个示例。实际上,康涅狄格是一个巨大的矩阵。这是一个5000行x 15列的矩阵,由许多数据块组成,但是我不能理解这段代码。。。。“匹配”来自哪里,匹配的含义是什么??有“nmatches”和“matches”,但我无法理解这两个变量的含义。另外,我是否需要制作新的a~d数组?我可以直接把I和l或者a和b与矩阵“conn”进行比较吗?匹配是个错误,应该是nums。nums只是一个数组,包含您要查找的数字。我也编辑过。谢谢,但我不能理解这个代码。。。。“匹配”来自哪里,匹配的含义是什么??有“nmatches”和“matches”,但我无法理解这两个变量的含义。另外,我是否需要制作新的a~d数组?我可以直接把I和l或者a和b与矩阵“conn”进行比较吗?匹配是个错误,应该是nums。nums只是一个数组,包含您要查找的数字。我也编辑过。