Re: Do loop -- replace with array ops?
- From: "highegg" <highegg@xxxxxxxxx>
- Date: 6 Nov 2006 04:46:03 -0800
PhilB wrote:
Simple question: can I make this neater? (ie replace the loop with array
operations)
Background:
we have the integer array subtemp(1:nsubtemp,1:3)
and the data structure s. subtemp contains data from a particular file,
and having read it in we want to put it into the data structure properly
(don't ask why I have to do it like this).
The relevant part of s is s%sub(1:nsub) where nsub<nsubtemp.
k=0
DO j=1,nsubtemp
IF( subtemp(j,1)==s%ifile )THEN
k=k+1
IF( k>s%nsub )THEN
(error handling thing)
END IF
s%sub(k)%ig = subtemp(j,2)
s%sub(k)%isub = subtemp(j,3)
END IF
END DO
I've tried a WHERE:
WHERE( subtemp(:,1)==s%ifile )
s%subh%ig = subtemp(:,2)
s%subh%isub = subtemp(:,3)
END WHERE
(won't work because the arrays aren't conformable, because nsub<nsubtemp.)
I've tried a FORALL:
FORALL( j=1:nsubtemp, k=1:xenosub(i)%nfsub, subtemp(j,1)==s%ifile )
s%subh(k)%ig = subtemp(j,2)
s%subh(k)%isub = subtemp(j,3)
END FORALL
(won't work because it uses every combination of j,k rather than one
unique k-value for every j that passes the mask)
Sorry if I'm being thick -- I just feel that I aught to be able to do
this neater than with a DO loop...
Thanks,
PhilB
Well, you can use
nmask = count(subtemp(:,1)==s%ifile)
IF (nmask > s%nsub) THEN
(error handling thing)
END IF
s%subh%ig(1:nmask) = PACK(subtemp(:,2),subtemp(:,1)==s%ifile)
s%subh%isub(1:nmask) = PACK(subtemp(:,3),subtemp(:,1)==s%ifile)
but I'd like to say that there is nothing wrong with DO loops in modern
Fortran,
and a lot of people would certainly consider the DO version more
readable,
not speaking of the fact that it has a good chance of being faster.
Jaroslav
.
- Follow-Ups:
- Re: Do loop -- replace with array ops?
- From: PhilB
- Re: Do loop -- replace with array ops?
- References:
- Do loop -- replace with array ops?
- From: PhilB
- Do loop -- replace with array ops?
- Prev by Date: Do loop -- replace with array ops?
- Next by Date: Re: getting g95 running on my macbook (OS X, intel chip)
- Previous by thread: Do loop -- replace with array ops?
- Next by thread: Re: Do loop -- replace with array ops?
- Index(es):
Relevant Pages
|