Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29429 Discussions

Subsetting an array with unknown array size

Harry_G_
Beginner
1,414 Views

I want to select elements in an array that meet a certain condition.

program subsetting

implicit none
real,dimension(100)::A
! I declare an array(say,B) elements of which are greater than 0.15 in A

call random_number(A)
! I have to select elements that are greater than 0.15 and store them in B

end program subsetting

The problem here is that I don't know how many elements are greater than 0.15. How can I write a code for getting the array B?

0 Kudos
1 Solution
Steven_L_Intel1
Employee
1,414 Views
program subsetting

implicit none
real,dimension(100)::A
! I declare an array(say,B) elements of which are greater than 0.15 in A
real, allocatable, dimension(:) :: B

call random_number(A)
! I have to select elements that are greater than 0.15 and store them in B
B = PACK(A,(A > 0.15))
print *, B

end program subsetting

Compile with /standard-semantics (Fortran > Language > Enable F2003 Semantics)

View solution in original post

0 Kudos
2 Replies
Steven_L_Intel1
Employee
1,415 Views
program subsetting

implicit none
real,dimension(100)::A
! I declare an array(say,B) elements of which are greater than 0.15 in A
real, allocatable, dimension(:) :: B

call random_number(A)
! I have to select elements that are greater than 0.15 and store them in B
B = PACK(A,(A > 0.15))
print *, B

end program subsetting

Compile with /standard-semantics (Fortran > Language > Enable F2003 Semantics)

0 Kudos
Harry_G_
Beginner
1,414 Views

PACK! Thanks you, Steve.

0 Kudos
Reply