子集数组与Fortran给出了一个条件? [英] subset array with Fortran given a condition?
问题描述
A = 11 22 43 55
15 56 65 63
54 56 32 78
我想用m-1列创建一个数组B, ).eq。 56
所以B应该是:
B = 15 65 63
54 32 78
我甚至不知道如何启动,例如, B的维度应该是动态确定的(我认为)
感谢您的帮助!
解决方案 函数extractB(A)result(B)
integer,dimension(:, :),intent(in):: A
integer,dimension(:, :),pointer :: B
integer :: nrowB,i,pos
nrowB = count(A(:,2)== 56)
allocate(B(nrowB,size(A,2)-1))
pos = 1
(A,1)
if(A(i,2)== 56)then
B(pos,1)= A(i,1)
B (pos,2 :) = A(i,3 :)
pos = pos + 1
end if
end do
end function extractB
您称之为
B = extractB(A)
用B定义:
integer,dimension(:, :),allocatable :: B
I假定您的数组的整数。如果你的编译器实现了指针作为返回值,你可以使用指针来代替allocatable。
====添加完整程序====
模块提取
包含
子程序testExtract(A,B)
双精度,维(:,:),intent(in):: A
double precision,dimension(:, :),intent(out),allocatable :: B
B = extractB(A)
结束子程序testExtract
函数extractB(A)结果(B)
双精度,维(:, :), intent(in):: A
double precision,dimension(:, :),allocatable :: B
integer :: nrowB,i,pos
nrowB = count(A (:,2)== 56)
allocate(B(nrowB,size(A,2)-1))
pos = 1
do i = 1,size(A,1 )
if(A(i,2)== 56)then
B(pos,1)= A(i,1)
B(pos,2 :) = A ,3 :)
pos = pos + 1
end if
end do
end function extractB
end模块提取
程序测试
使用提取
整数,参数:: n = 3
整数,参数:: m = 4
双精度,维(3,4):: A
双精度,维(:, :),allocatable :: B
A(1,:) = [11,22,43,55]
A(2,:) = [15,56,65,63]
A(3,:) = [54,56,32,78]
print *,'A:'
print *,int(A)
!B = extractB(A)
call testExtract(A,B)
print *,'B'
print *,int(B)
结束程序
Suppose I have an array A(n,m). Is it possible to subset that array in Fortran to create a new array? For example,
A = 11 22 43 55
15 56 65 63
54 56 32 78
I want to create an array B with m-1 columns and the rows that satisfies A(:,2) .eq. 56
So B should be:
B = 15 65 63
54 32 78
I don't even know how to start because, for example, the dimension of B should be determined dynamically (I think)
Thanks for the help!
解决方案 Are you looking for something like this?
function extractB(A) result(B)
integer, dimension(:,:), intent(in) :: A
integer, dimension(:,:), pointer :: B
integer :: nrowB, i, pos
nrowB = count( A(:,2)==56)
allocate( B(nrowB, size(A,2)-1 ) )
pos = 1
do i = 1, size(A,1)
if(A(i,2)==56)then
B(pos,1) = A(i,1)
B(pos,2:) = A(i,3:)
pos = pos+1
end if
end do
end function extractB
That you call like
B = extractB(A)
with B defined like:
integer, dimension(:,:), allocatable :: B
I assumed integer for your arrays. If your compiler implement pointer as return value, you can used pointers in the place of allocatable.
====adding a full program ====
module extract
contains
subroutine testExtract(A, B)
double precision, dimension(:,:), intent(in) :: A
double precision, dimension(:,:), intent(out), allocatable :: B
B = extractB(A)
end subroutine testExtract
function extractB(A) result(B)
double precision, dimension(:,:), intent(in) :: A
double precision, dimension(:,:), allocatable :: B
integer :: nrowB, i, pos
nrowB = count( A(:,2)==56)
allocate( B(nrowB, size(A,2)-1 ) )
pos = 1
do i = 1, size(A,1)
if(A(i,2)==56)then
B(pos,1) = A(i,1)
B(pos,2:) = A(i,3:)
pos = pos+1
end if
end do
end function extractB
end module extract
program test
use extract
integer, parameter :: n = 3
integer, parameter :: m = 4
double precision, dimension(3,4) :: A
double precision, dimension(:,:), allocatable :: B
A(1,:) = [11, 22, 43, 55]
A(2,:) = [15, 56, 65, 63]
A(3,:) = [54, 56, 32, 78]
print*, 'A :'
print*, int(A)
!B = extractB(A)
call testExtract(A, B)
print*, 'B'
print*, int(B)
end program
这篇关于子集数组与Fortran给出了一个条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文