- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I try to draw samples and calculate something by writing a subroutine.
! main file program mymain use mymodule implicit none real,dimension(10)::inputdata real,dimension(:,:),allocatable::popmat integer :: snr,pnr,i,nsimc real :: sdot integer, parameter :: nsim=100 real, dimension(nsim) :: sdotmat inputdata = (/1,2,3,4,5,6,7,8,9,10/) allocate(popmat(5,2)) popmat = transpose(reshape(inputdata,(/2,5/))) snr=3 pnr=size(popmat,1) do i=1,nsimc call resample(snr,pnr,popmat,sdot) sdotmat(nsimc) = sdot end do deallocate(popmat) print*, sdotmat end program mymain
The subroutine for simulation looks like:
module mymodule contains subroutine resample(snr,pnr,popmat,sdot) implicit none integer, intent(in) :: snr,pnr real, dimension(pnr,2), intent(in) :: popmat real, intent(out) :: sdot real, dimension(snr,2) :: smat integer :: m,i,l real :: r m=0 do while (m < snr) do i=1,pnr call random_number(r) l = int((real(pnr)-i+1)*r)+1 if (l .GT. (snr-m)) then cycle end if m=m+1 smat(m,1:2)=popmat(i,1:2) end do end do sdot = dot_product(smat(:,1),smat(:,2)) end subroutine resample end module mymodule
If I write a code to draw random samples INSIDE the subroutine, it works properly. But when I loop over the subroutine from the main, it is compiled, but execution produces an error. Would you let me know what problems are in the codes above? Thanks!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
With the main program corrected as follows, the program runs with no errors.
program mymain use mymodule implicit none real,dimension(10)::inputdata real,dimension(:,:),allocatable::popmat integer :: snr,pnr,i real :: sdot integer, parameter :: nsimc=100 real, dimension(nsimc) :: sdotmat inputdata = (/1,2,3,4,5,6,7,8,9,10/) allocate(popmat(5,2)) popmat = transpose(reshape(inputdata,(/2,5/))) snr=3 pnr=size(popmat,1) do i=1,nsimc call resample(snr,pnr,popmat,sdot) sdotmat(i) = sdot end do deallocate(popmat) print*, sdotmat end program mymain
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The variable nsimc is used before its value has been set. On Line-18 of the main program, the subscript should probably be the loop variable i rather than nsimc. Perhaps, you meant "nsimc" and "nsim" to be one and the same.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@mecej4: Thanks. I fixed the small error for the nsimc.
do nsimc=1,nsim
call resample(snr,pnr,popmat,sdot)
sdotmat(nsimc) = sdot
end do
Now it works. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
With the main program corrected as follows, the program runs with no errors.
program mymain use mymodule implicit none real,dimension(10)::inputdata real,dimension(:,:),allocatable::popmat integer :: snr,pnr,i real :: sdot integer, parameter :: nsimc=100 real, dimension(nsimc) :: sdotmat inputdata = (/1,2,3,4,5,6,7,8,9,10/) allocate(popmat(5,2)) popmat = transpose(reshape(inputdata,(/2,5/))) snr=3 pnr=size(popmat,1) do i=1,nsimc call resample(snr,pnr,popmat,sdot) sdotmat(i) = sdot end do deallocate(popmat) print*, sdotmat end program mymain

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page