Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

where construct 1

Status
Not open for further replies.

mikibelavista

Technical User
Jan 18, 2012
32
I have problem that I have two arrays and I want to associate their arguments.But I have not managed this with where construct.
real,dimension(100) :: d,v
d has 6 different values,if d is 3,how to take all array elements of v?
 
I'd advise that you should clarify what you want to do.

You specify d to be an array of 100 elements, so d cannot be just 3.

And to get the elements of v from some file or keyboard input should not be affected by the type declaration and values of d at all.

Norbert


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Not very sure of what you're trying to say. Could you write out what you are trying to do as a do loop? It is easier to convert to a where clause when you know what is required.
 
I have solved this without where construct like this
sum1=0
n1=0
n2=0
do i=1,9
read(10,*)d(i)
read(11,*)v(i)
if (d(i)==3) then
s1=v(i)
n1=n1+1
sum1=sum1+s1
end if
if (d(i)==2) then
s2=v(i)
n2=n2+1
sum2=sum2+s2
end if
end do
av1=sum1/n1
av2=sum2/n2
write(*,*)sum1,sum2,n1,n2,av1,av2
 
It would have been easier without a where clause. The problem with the where clause is that non arrays cannot appear in it. You have to do something expensive like
Code:
program where1
integer d(9)
real    v(9)
real    s3(9), s2(9), n3s(9), n2s(9) 

read (10,*) d
read (11,*) v

! Initialize the counters
s2 = 0.0
s3 = 0.0
n2s = 0.0
n3s = 0.0

where (d .eq. 3)
   s3 = v
   n3s = 1.0
else where (d .eq. 2)
   s2 = v
   n2s = 1.0
end where
av1=sum(s3)/sum(n3s)
av2=sum(s2)/sum(n2s)
write(*,*)av1,av2 
stop
end
 
Alternatively use dot products, but still quite expensive
Code:
program where1
integer d(9)
real    v(9)
real    n3s(9), n2s(9)

read (10,*) d
read (11,*) v

! Initialize the counters
n2s = 0.0
n3s = 0.0

where (d .eq. 3)
   n3s = 1.0
else where (d .eq. 2)
   n2s = 1.0
end where
av1=dot_product(v,n3s)/sum(n3s)
av2=dot_product(v,n2s)/sum(n2s)
write(*,*)av1,av2
stop
end
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top