How to declare a variable mid-routine in Fortran

后端 未结 4 1132
难免孤独
难免孤独 2020-12-07 03:56

I would like to create an array with a dimension based on the number of elements meeting a certain condition in another array. This would require that I initialize an array

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-07 04:13

    Try this

    real, dimension(50) :: some_array
    real, dimension(:), allocatable :: other_array
    integer :: status
    ...
    allocate(other_array(count(some_array>0)),stat=status)
    

    at the end of this sequence of statements other_array will have the one element for each element of some_array greater than 0, there is no need to write a loop to count the non-zero elements of some_array.

    Following @AlexanderVogt's advice, do check the status of the allocate statement.

提交回复
热议问题