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!

comparing arrays with different dimensions 2

Status
Not open for further replies.

mikibelavista

Technical User
Jan 18, 2012
32
I have program like this
program comp
implicit none
integer :: i
real,dimension(17) :: a
real,dimension(29) :: b,c
open(10,file='01.txt',status='old')
open(11,file='frekvencije.dat',status='old')

read(10,*)a
read(11,*)b

do i=1,29
if (b(i)==a(1)) then
c(i)=i
end if
end do
write(*,*)c
end program
How to solve this,I want to compare b with a,but how to do this foe every a?
I have tried with WHERE but it did not work.
 
Depends what you want to achieve...a single b could match several a's...or several b's could match a single a....which one do you want to catch? the first encountered? the last one? all? The way I see it, you do not have enough room to record such info...then again, it could be that your data follows some specific pattern.

It may be that all you need are two nested do-loops, one to vary b(i) and another one to vary a(j)...but I am not sure.

Please illustrate. Instead of 17 and 29...will you please set up a smaller problem, say, 5 and 9 and show the input data and what it is that you would like to see as the output data?

 
ok.For example a=(/2,5,6,7,9/) and b=(/1,2,3,4,5,6,7,8,9) What I want is to get c=(/0,2,0,0,5,6,7,0,9,/).
I had tried with nested do looped but it didn't work well.
 
As I understand your example, you do not actually want compare those two arrays but check if any element of b is present in a.

First of all: I doubt if this could be done with reals. For equality means that the two entities you compare are equal bit for bit. This can produce any amount of problems if the elements of your arrays are results of prior evaluation in your program.

So in the code snippet I would assume the arrays to be integers:

Code:
...
...
c = 0
do jb = 1, 29
    do ja = 1, 17
       if (a(ja) == b(jb)) then
           c(jb) = jb
           exit
       endif
    enddo
enddo
write (*,*) c

This would produce an array of indices of elements of array b that are present anywhere in array a.

Norbert



The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Thanks,I have done it this way.
program comp
implicit none
integer :: i,n
real,dimension(17) :: a
real,dimension(29) :: b,c

open(10,file='01.txt',status='old')
open(11,file='frekvencije.dat',status='old')

read(10,*)a
read(11,*)b
do 20 n=1,17
do i=1,29
if (b(i)==a(n)) then
c(i)=i
end if
end do
20 continue
write(*,*)c
end program
 
Your code stills shows your variables as being declared as "real"...as recommended by Norbert, you really need to declare them "integer" if they are (and they should be) integer, but, most importantly, if you are going to be using the equality operator..."real" numbers are rarely equal, to compare them you need tolerance.

Also, in his snippet of code, Norbert shows the array 'c' being initialized to zero, this is necessary because during the nested loops, not all c(i) items are visited and set...some are left un-initialized.

In any case, you could replace your 7 lines of nested do loops with the following line,too:
Code:
    forall(n=1:5, i=1:9, b(i)==a(n)) c(i+n-n)=i
this one-liner would run faster than the nested do-loops, important if the problem gets large.
 
STILL I HAVE PROBLEMS
module m4
contains
subroutine mm(iter,n,b)
integer,intent(in) :: iter,n
integer,dimension(33),intent(in) :: b
integer :: id1,id2,i,x
integer,dimension:)),allocatable :: a
integer,dimension(33) :: c
character(6) :: tfile=" .nmf"
character(6) :: pfile=" .pos"

allocate(a(n))
id1=iter/10
id2=iter-10*id1
tfile(1:2)=CHAR(id1+48)//CHAR(id2+48)
pfile(1:2)=CHAR(id1+48)//CHAR(id2+48)
open(30, file=tfile)
open(31, file=pfile)

do x=1,n
read(30,*)a(x)
end do

forall(x=1:n, i=1:33,b(i)==a(x))
c(i+x-x)=i
end forall
write(31,88)c
88 format(i8)

deallocate(a)
end subroutine
end module
14.nmf
765677
571038
425877
317617
236877
176662
131753
98261
73282
54653
40760
30399
22671
16908
12610
9404
7013
5230
3901
2909
2169
1618
1206
900
671
500
373
b:
4449683
3318550
2474959
1845812
1376598
1026660
765677
571038
425877
317617
236877
176662
131753
98261
73282
54653
40760
30399
22671
16908
12610
9404
7013
5230
3901
2909
2169
1618
1206
900
671
500
373

WHY I GOT THIS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 
forall(x=1:n, i=1:33,b(i)==a(x))
c(i+x-x)=i
end forall
write(31,88)c

You just set c(i) = i write it.

Norbert

The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
No, take a look:
do x=1,n
read(30,*)a(x)
end do

forall(x=1:n, i=1:33,b(i)==a(x))
c(i+x-x)=i
end forall

do i=1,33
write(31,88)c(i)
88 format(i8)

I got exactly the same 33 instead of 27 values.
 
SOLVED:
forall(x=1:n, i=1:33,a(x)==b(i))
c(x+i-i)=i
end forall
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top