Home > Archive > Fortran > June 2005 > pack function for 2D matrix in fortran?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
pack function for 2D matrix in fortran?
|
|
|
| Dear Fortran experts,
I would like to locate location of matrix elements which has a certain
criteria (example ==1.0) with fortran.I have read this mailing list but
I haven't got an appropiate axample. Could you plaese help me?
Example:
A(3,4)= 1.0 0.0 0.0 1.0
0.0 1.0 1.0 0.0
0.0 1.0 1.0 1.0
I would like to store the locations of 1.0, so I have
[c,d]=
1,1
2,2
3,2
2,3
3,3
1,4
3,4
where c=/1 2 3 2 3 1 3/ and d=/1 2 2 3 3 4 4/
Thank you for your help.
agus
| |
| Arjen Markus 2005-06-09, 3:58 pm |
| akang wrote:
>
> Dear Fortran experts,
> I would like to locate location of matrix elements which has a certain
> criteria (example ==1.0) with fortran.I have read this mailing list but
> I haven't got an appropiate axample. Could you plaese help me?
> Example:
>
> A(3,4)= 1.0 0.0 0.0 1.0
> 0.0 1.0 1.0 0.0
> 0.0 1.0 1.0 1.0
>
> I would like to store the locations of 1.0, so I have
>
> [c,d]=
> 1,1
> 2,2
> 3,2
> 2,3
> 3,3
> 1,4
> 3,4
>
> where c=/1 2 3 2 3 1 3/ and d=/1 2 2 3 3 4 4/
>
> Thank you for your help.
> agus
Hm, I suggest trying something like:
do j = 1,size(a,2)
do i = 1,size(a,1)
ai(i,j) = i
aj(i,j) = j
enddo
enddo
c = pack( ai, a == 1.0 )
d = pack( aj, a == 1.0 )
But as I have never used PACK() I am merely guessing ...
Regards,
Arjen
| |
| Rich Townsend 2005-06-09, 3:58 pm |
| akang wrote:
> Dear Fortran experts,
> I would like to locate location of matrix elements which has a certain
> criteria (example ==1.0) with fortran.I have read this mailing list but
> I haven't got an appropiate axample. Could you plaese help me?
> Example:
>
> A(3,4)= 1.0 0.0 0.0 1.0
> 0.0 1.0 1.0 0.0
> 0.0 1.0 1.0 1.0
>
>
> I would like to store the locations of 1.0, so I have
>
> [c,d]=
> 1,1
> 2,2
> 3,2
> 2,3
> 3,3
> 1,4
> 3,4
>
> where c=/1 2 3 2 3 1 3/ and d=/1 2 2 3 3 4 4/
>
> Thank you for your help.
> agus
>
c = PACK(SPREAD((/1,2,3/), DIM=2, NCOPIES=4), MASK=(A == 1.0))
d = PACK(SPREAD((/1,2,3,4/), DIM=1, NCOPIES=3), MASK=(A == 1.0))
Demonstration program attached.
cheers,
Rich
| |
| glen herrmannsfeldt 2005-06-09, 3:58 pm |
| Rich Townsend wrote:
> akang wrote:
(snip)
[color=darkred]
> c = PACK(SPREAD((/1,2,3/), DIM=2, NCOPIES=4), MASK=(A == 1.0))
> d = PACK(SPREAD((/1,2,3,4/), DIM=1, NCOPIES=3), MASK=(A == 1.0))
That looks pretty neat. I wonder how efficient it is compared
to a more ordinary method using DO loops. Though for small arrays
efficiency isn't likely important. For larger arrays the question
of what dimension to give C and D becomes more important, and the
fact that the above code likely generates some temporary arrays.
Otherwise, I might have suggested DO loops to count the number of
matching array elements, allocate c and d, and then loops again
to store the appropriate values.
One should probably add the usual caution about comparing floating
point values of unknown source for equality.
-- glen
| |
|
| Thanks Arjen, Rich and Glen.
I have tried all of your suggestions...
In my case, I have a very large matrix ( A(m,n), where m is an
allocated parameter and n is 50000) containing 0.0 and 1.0. I have
successfully located the locations of 1.0 elements using DO loops..but
I can't figure out, how to store the result in arrays [c,d]. Would you
please help me?
Best Regards,
agus
| |
| Arjen Markus 2005-06-10, 4:00 pm |
| akang wrote:
>
> Thanks Arjen, Rich and Glen.
> I have tried all of your suggestions...
> In my case, I have a very large matrix ( A(m,n), where m is an
> allocated parameter and n is 50000) containing 0.0 and 1.0. I have
> successfully located the locations of 1.0 elements using DO loops..but
> I can't figure out, how to store the result in arrays [c,d]. Would you
> please help me?
> Best Regards,
> agus
I assume that you want to know how large the arrays c and d should be
....
Try:
sizecd = count( a == 1.0 )
This will give you the number of entries equal to one. You can
use this to allocate the arrays c and d (or if you like an array
cd with shape (sizecd,2)).
Then
c = pack( .... )
d = pack( .... )
(or:
cd(:,1) = pack( .... )
cd(:,2) = pack( .... ) )
will give you exactly what you need.
Regards,
Arjen
| |
|
| It works !
Thanks a lot.
cheers,
agus
| |
| glen herrmannsfeldt 2005-06-10, 4:00 pm |
| Arjen Markus wrote:
(snip)
> I assume that you want to know how large the arrays c and d should be
> Try:
> sizecd = count( a == 1.0 )
Can most compilers do this without a temporary array?
It wouldn't seem hard, but I am not so sure.
-- glen
|
|
|
|
|